2016-11-07 51 views
0

這是search.php的代碼。我想按如下方式在輸入框中按日期搜索。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> 

這是我的訪問者print.php代碼,我得到的DAT未定義指數錯誤(這是從其他網頁)

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

問題: 注意:未定義指數:DAT在d :\ wamp \ www \ radissun訪客\ visitor-print.php在線12 並沒有顯示來自mysql的數據

+1

MISSING表單提交 JYoThI

+1

JYoThI

+0

不應'mysql_query($ sql,$ conn)'爲'mysql_query($ sql)'您已連接所以不需要將連接字符串添加到查詢中...... – NewToJS

回答

0

您正在尋找的答案:您正在使用$ _POST - 這只是用於具有方法的表單= 「後」。默認方法是GET - 所以你的變量是$ _GET []。您可以將html中的方法參數設置爲POST,或者將PHP代碼更改爲GET。

但是我必須指出,你的代碼不適用於生產環境,因爲它很容易受到SQL注入的影響。見http://www.w3schools.com/sql/sql_injection.asp

+0

謝謝大家的回覆 所有的答案都對我非常有幫助 –

0

表格METHOD="POST"

NOTE:缺少方法如果使用METHOD="POST"你可以得到珍視$ _ POST [ '逸'];如果你不使用方法屬性.FORM只是作爲GET方法提交,你可以通過$ _GET獲取['dat'];

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

MISSING表單提交

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

(OR)

ADD TYPE="SUBMIT" TO BUTTON

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

默認情況下按鈕的類型是「submit」。 – Gerfried