简体   繁体   English

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

[英]How to search a record using a mySql unique ID / column name?

The following code works perfectly. 以下代码可以完美运行。 It gives hotel url of the booking ID RRGPGM68 它提供预订ID RRGPGM68 hotel url

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

But I want to assign the booking_id_serch_text dynamically from a text input field. 但是我想从文本输入字段动态分配booking_id_serch_text So I altered the code as follows: 因此,我将代码更改如下:

<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);
}

?>

But the above code just does nothing. 但是上面的代码什么都不做。 How can I make it work? 我该如何运作? I'll add validation after this purpose is archived. 存档此目的后,我将添加验证。

您必须在按下search使用提交按钮来发布表单

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

You are not submitting the form 您没有提交表格

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

should be 应该

<input type="submit" value="search" name="search">
<input type="button" /> buttons 

will not submit a form - they don't do anything by default. 将不会提交表单-默认情况下,他们不会做任何事情。 They're generally used in conjunction with JavaScript as part of an AJAX application. 它们通常与JavaScript一起用作AJAX应用程序的一部分。

<input type="submit"> buttons 

will submit the form they are in when the user clicks on them, unless you specify otherwise with JavaScript. 除非您用JavaScript另行指定,否则当用户单击它们时,将提交它们所在的表单。

So use here 所以在这里用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM