如何使用mySql唯一ID /列名搜索记录?

时间:2014-09-19 08:28:16

标签: php mysql sql search

以下代码完美无缺。它会提供hotel url的预订ID RRGPGM68

<?PHP
$con=mysqli_connect("localhost","username","pasword","dbName");
        // Check connection
        if (mysqli_connect_errno()) {
          die ("Failed to connect to MySQL: " . mysqli_connect_error());
        }
$booking_id_serch_text = RRGPGM68;
$searchroute = "select * from booking_tbl where booking_nmbr = '$booking_id_serch_text'";
$result = $con->query($searchroute);
$row = $result->fetch_assoc();
echo 'Hotel URL: '.$row['hotel_url'];
mysqli_close($con);
?>

但我想从文本输入字段动态分配booking_id_serch_text。所以我改变了代码如下:

<form action="<?php echo get_permalink();?>" method="post">
    <input type="text" placeholder="Search..." required name="search_text">
    <input type="button" value="search" name="search">
</form>

<?PHP

if($_POST["search"]){
$serch_text = $_POST["search_text"]; 
$con=mysqli_connect("localhost","username","pasword","dbName");
        // Check connection
        if (mysqli_connect_errno()) {
          die ("Failed to connect to MySQL: " . mysqli_connect_error());
        }       
$booking_id_serch_text = $serch_text;
$searchroute = "select * from booking_tbl where booking_nmbr = '$booking_id_serch_text'";
$result = $con->query($searchroute);
$row = $result->fetch_assoc();
echo 'Hotel URL: '.$row['hotel_url'];
mysqli_close($con);
}

?>

但上面的代码什么也没做。 我怎样才能使它工作? 我会在归档此目的后添加验证。

3 个答案:

答案 0 :(得分:1)

在按search

时,您必须使用提交按钮制作表单
<input type="submit" value="search" name="search">

答案 1 :(得分:1)

您没有提交表格

<input type="button" value="search" name="search">

应该是

<input type="submit" value="search" name="search">

答案 2 :(得分:1)

<input type="button" /> buttons 

不会提交表格 - 默认情况下他们不会做任何事情。它们通常与JavaScript结合使用,作为AJAX应用程序的一部分。

<input type="submit"> buttons 

将在用户点击时提交他们所在的表单,除非您使用JavaScript另行指定。

所以在这里使用