如何显示来自mySQL的正确数据?

时间:2018-06-24 19:10:57

标签: php html mysql xampp

我正在尝试查询数据库并显示结果。该查询基于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>

感谢您的帮助。预先谢谢你!

1 个答案:

答案 0 :(得分:1)

您正在使用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 Injection Attack开放   甚至if you are escaping inputs, its not safe!   在MYSQLI_PDO API中使用prepared parameterized statements