javascript - PHP - 通知 : Undefined index: and not displaying data

标签 javascript php jquery html mysql

这是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>

这是我的访问者打印.php 代码,我在 dat 上收到 undefined index 错误(来自其他页面)

<?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>

问题: 注意:未定义索引:第 12 行 D:\wamp\www\radissun guest\visitor-print.php 中的 dat 并且不显示来自 mysql 的数据

最佳答案

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

但我必须指出,您的代码无法用于生产环境,因为它容易受到 SQL 注入(inject)攻击。请参阅http://www.w3schools.com/sql/sql_injection.asp

关于javascript - PHP - 通知 : Undefined index: and not displaying data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40463544/

相关文章:

javascript - 呈现为未定义,无法完全找出错误

php - 使用一个连接读取 FTP 目录中每个文件的内容

javascript - jquery 递增索引输入数组

javascript - 在页面刷新或后退/前进浏览器导航时重置下拉菜单

javascript - 为什么 $(this) 在点击时正确调用但在悬停时不正确?

javascript - 检测谁更改了模型(用户输入与 Controller )angularjs

javascript - 防止unpoly改变浏览器历史记录

javascript - 使用正则表达式在 css 文件中查找字体

javascript - 从外部 javascript 获取动态变量

php - 在 Woocommerce 中将结帐国家/地区下拉菜单设置为只读