2017-10-12 82 views
0
<html> 
    <head> 
     <style> 
      input 
      { 
       position:relative; 
       height:5%; 
       width:25%; 
       border:2px solid; 
       background-color:#219869; 
       border-radius:10px; 
       font-weight:bold; 
       text-align:center; 
       font-size:19px; 
      } 
     </style> 
    </head> 

    <body bgcolor='#b5eff5'> 
     <form method="POST" action='criminal_search.php'> 
      <select name='s1'> 
       <option value="ID">ID</option> 
       <option value="Name">Name</option> 
       <option value="Phone Number">Phone Number</option> 
       <option value="Date">Date</option> 
      </select> 
      <input type=text name='i1' id='i1'></input> 
      <input type=submit value=submit name="submit"></input> 
     </form> 
    </body> 
    <?php 
     if(isset($_POST['submit'])) 
     { 
      $dbname = "policainformation"; 
      $conn = mysqli_connect('localhost','root','',$dbname); 
      if (!$conn) 
      { 
       die("Connection failed: " . mysqli_connect_error()); 
      } 
      else 
      { 
       $option1=$_POST['s1']; 
       if($option1=="ID") 
       { 
        $option1="report_id"; 
       } 
       else 
       if($option1=="Name") 
       { 
        $option1="reporter_name"; 
       } 
       else 
       if($option1=="Phone Number") 
       { 
        $option1="reporter_ph"; 
       } 
       else 
       if($option1=="Date") 
       { 
        $option1="report_date"; 
       } 

       $option2=$_POST['i1']; 
       if($option1!="Name") 
       $sql="select * from criminal where $option1 in ($option2)"; 
       else 
       $sql="select * from criminal where $option1 in '$option2'"; 
       $result=mysqli_query($conn, $sql) or die("<script type='text/javascript'>alert('No Records Found!')</script>"); 
       if($result) 

       if (mysqli_num_rows($result) > 0) 
       { 
        while($row = mysqli_fetch_assoc($result)) 
        { 
         echo $row["report_id"].$row["reporter_name"].$row["reporter_ph"].$row["reporter_email"].$row["reporter_address"].$row["report_date"].$row["suspect_name"].$row["reason_of_report"].$row["proof"]."<br>"; 
        } 
       } 
       else 
       { 
        echo "<script type='text/javascript'>alert('No Records Found!')</script>"; 
       } 
      } 
     } 
    ?> 
</html> 

這是我的HTML代碼。我正在嘗試檢索值並在網頁上顯示。除了NAME選項,一切正常。當我用這個名字進行搜索時,即當我用PHP代碼中的名稱進行查詢時,它不起作用。它迴應沒有找到記錄。請幫幫我。我也把表結構爲更好的參考。數據庫錯誤名稱未提取

https://gyazo.com/1c69aad3491c00d876128e198e39f176

https://screenshots.firefox.com/psi7MgWUjapXavID/localhost

+0

'IN'功能需要括號,你的第二個具有單引號。你的目標是什麼if/else? – Scuzzy

+0

if else是否用於執行特定查詢。如果它是名稱,那麼sql必須爲文本引用單引號,如果是數字,則不必使用單引號。因此給出了其他條件。但它給其他部分帶來了問題。 –

+0

@Scuzzy即使我把括號 –

回答

0

T-SQL中使用括號()沒有引號 ''

$sql="select * from criminal where $option1 in ($option2)"; 
else 
$sql="select * from criminal where $option1 = '$option2'"; 
+0

仍然不工作仍然工作 –

+0

數據庫是在mysql –

+0

你不需要IN ....我更新了第二個查詢=(等於) –