首页 > 解决方案 > 如何显示来自 mySQL 的正确数据?

问题描述

我正在尝试查询数据库并显示结果。查询基于来自 html 表单的信息。但是,当我在表单中输入一个名称(例如 john)时,该表有 2 个具有该名称的条目,我得到 0 个结果。我不知道问题是什么。

这是html表单:

<form action="cust_details_search.php" method="post">         
Name :
<input type="text" name="name_search" id="name_search" >
Email :
<input type="email" name="email_search" id ="email_search" >

Phone no. :

 <input type="phone" name="phone_search" id="phone_search" >


Address :
<input type="text" name="address_search" id="address_search" >

City :
<input type="text" name="city_search" id="city_search" >

State :
<input type="text" name="state_search" id="state_search" >
<br> <br>
Country :
<input type="text" name="country_search" id="country_search">

Product Enquired for :
<input type="text" name="prod_search" id="prod_search">  

<input type="submit" value="Submit">

</form>

和 php 文件:

 <?php 
$server = "127.0.0.1";
$dbUsername = "root";
$dbPassword = "";

//create connection
$dbconn = new mysqli($server, $dbUsername, $dbPassword, $dbname);

 $name_search = $_POST['name_search'];
 $email_search = $_POST['email_search'];
 $phone_search = $_POST['phone_search'];
 $address_search = $_POST['address_search'];
 $city_search = $_POST['city_search'];
 $state_search = $_POST['state_search'];
 $country_search = $_POST['country_search'];
 $prod_search = $_POST['prod_search'];

$run_query = mysqli_query($dbconn, 
            "SELECT * 
            FROM CustomerDetails 
            WHERE (Name  LIKE '%.$name_search.%') 
            OR (`Phone no.` LIKE '%.$phone_search.%') 
            OR (`Address` LIKE '%.$address_search.%') 
            OR (`City` LIKE '%.$city_search.%') 
            OR (`State` LIKE '%.$state_search.%') 
            OR (`Country` LIKE '%.$country_search.%') 
            OR (`Product Enq. For` LIKE '%.$prod_search.%') 
            OR (`Email` LIKE '%.$email_search.%')");

?>
<html>
<head>
<title>Search Resutls</title>
<style>
body {
background-color: rgb(131,41,54);
}
h1 { color:#FFFFFF
        }
h2 {  color:#FFFFFF
        }
p {  color:#FFFFFF
        } 

</style>

</head>
<body>
<center>
<h2> Customer Details </h2>
 <table style="width:100%">
<thead>
<tr>
    <th>Name</th>
    <th>Email</th>
    <th>Phone no. </th>
    <th>Address </th>
    <th>City </th>
    <th>State </th>
    <th>Country</th>
    <th>Product Enquired for </th>
    <th>Follow up details </th>
    </tr>
    </thead>
    <tbody>
<?php 
while($result = mysqli_fetch_assoc($run_query)) {
 ?>
<tr>
<td><?php echo $result['Name'] ?> </td>
<td><?php echo $result['Email'] ?></td>
<td><?php echo $result['Phone no.'] ?></td>
<td><?php echo $result['Address'] ?></td>
<td><?php echo $result['City'] ?></td>
<td><?php echo $result['State'] ?></td>
<td><?php echo $result['Country'] ?></td>
<td><?php echo $result['Product Enq. For'] ?></td>
<td><?php echo $result['Follow Up'] ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</center>
</body>
</html>

任何帮助表示赞赏。先感谢您!

标签: phphtmlmysqlxampp

解决方案


您正在使用 PHP 连接器.,但您不需要使用双引号字符串。$variables在双引号字符串中自动扩展,因此请尝试

$run_query = mysqli_query($dbconn, 
            "SELECT * 
            FROM CustomerDetails 
            WHERE (Name  LIKE '%$name_search%') 
            OR (`Phone no` LIKE '%$phone_search%') 
            OR (`Address` LIKE '%$address_search%') 
            OR (`City` LIKE '%$city_search%') 
            OR (`State` LIKE '%$state_search%') 
            OR (`Country` LIKE '%$country_search%') 
            OR (`Product Enq For` LIKE '%$prod_search%') 
            OR (`Email` LIKE '%$email_search%')");

你的一些列名也有一个.吗?我假设这些列名实际上不包含点。但是,如果这样做,我建议您通过编辑架构来删除它们。

您的脚本很容易受到SQL 注入攻击 即使您正在转义输入,它也不安全!在API或API中 使用准备好的参数化语句MYSQLI_PDO


推荐阅读