php - 如何显示来自 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>
任何帮助表示赞赏。先感谢您!
解决方案
您正在使用 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
推荐阅读
- python - if == 条件未在 django 模板中输出预期结果
- python - Django - 将复选框输入保存到数据库并更新页面
- angular - 同时在同一工作区上构建 Angular 库和应用程序
- node.js - 用于 Node 的 Typescript 库作为 NPM 中的混合包
- python - 在我的本地机器上安装 pyobjc-framework-Quartz 6.2.2 失败
- android - 无法使用 React Native 应用程序将图像上传到 Django 服务器
- python - Python中的列表排序算法不运行
- laravel - 设置 laravel 项目
- java - 如果出现异常,如何停止 graphql-spqr-spring-boot-starter 打印日志?
- angular - 在可观察的 HTTP 调用中实现加载器的正确方法?