PHP -注意:未定义索引:不显示数据

PHP - Notice: Undefined index: and not displaying data

本文关键字:显示 数据 索引 未定义 注意 PHP      更新时间:2023-09-26

这是search.php的代码。我想从输入框中按日期进行搜索,如下所示。

<form action="visitor-print.php">
    <div class="col-sm-3 text-center"><h3>Date</h3>
        <input type="text" class="form-control" name="dat" placeholder="Enter Date">
        <p class="help-block">Month Format 1,2,3,....29,30..</p>
        <button class="btn btn-default"><span>Submit</span></button>
    </div>
   </form>

这是我的visit -print.php代码,我在数据上得到未定义的索引错误(来自另一个页面)

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db('visitor_list');
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
$sql = "SELECT * FROM list1 WHERE d1 = '". $_POST["dat"] . "' ";
$retval = mysql_query(  $sql,$conn ) or die("Error: ".mysql_error($conn));
?>
 <div class="container">  
            <h3 class="text-center">User Feed-Back Details</h3><br /> 
            <div class="table-responsive" id="employee_table">  
                 <table class="table table-bordered">  
                      <tr>  
                           <th width="10%">Visitor Name</th>    
                           <th width="10%">Address</th>  
                           <th width="10%">Area to Visit</th>  
                           <th width="10%">Phone No.</th> 
                           <th width="20%">Want to meet with </th> 
                           <th width="50%">Purpose of meeting</th>  
                      </tr>  
                      <?php   
                      while($row = mysql_fetch_array($retval,MYSQLI_BOTH))  
                      {  
                      ?>  
                      <tr>  
                           <td><?php echo $row['nm']; ?></td>  
                           <td><?php echo $row['add1']; ?></td>  
                           <td><?php echo $row['area_vis']; ?></td>  
                           <td><?php echo $row['y1']; ?></td> 
                           <td><?php echo $row['app_ty']; ?></td>
                           <td><?php echo $row['no_per']; ?></td> 
                      </tr>  
                      <?php                           
                      }  
                      ?>  
                 </table>  
            </div>  
            <div align="center">  
                 <button name="create_excel" id="create_excel" class="btn btn-success">Create Excel File</button>  
            </div>  
       </div>
       <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>  
       <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
       <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> 
    <script>  
 $(document).ready(function(){  
 $('#create_excel').click(function(){  
       var excel_data = $('#employee_table').html();  
       var page = "excel.php?data=" + excel_data;  
       window.location = page;  
  });  
});  
</script>

问题:注意:未定义的索引:数据在D:'wamp'www'radissun visitor'visitor-print.php第12行并没有显示数据从mysql

您正在寻找的答案:您正在使用$_POST -这仅适用于具有method="post"的表单。默认方法是GET,所以变量是$_GET[]。您可以将html中的方法参数设置为POST,也可以将PHP代码更改为GET。

但是我必须指出,您的代码不能用于生产环境,因为它容易受到SQL注入的攻击。见http://www.w3schools.com/sql/sql_injection.asp

表单METHOD="POST"缺少方法

NOTE:如果您使用METHOD="POST",您可以通过$_POST['dat']获取值;如果你没有使用方法属性。form只是作为GET方法提交,你可以通过$_GET['dat'];

<form action="visitor-print.php" METHOD="POST" >

缺少表单提交

<INPUT TYPE="SUBMIT" NAME="SUBMIT" VALUE="SUBMIT" >

(或)

添加TYPE="SUBMIT"到按钮

<button TYPE="SUBMIT" class="btn btn-default"><span>Submit</span></button>