如何使用PHP和Mysql将搜索框放入数据库表中
我已经在数据库中创建了“客户信息”。我还把它放在customers.php页面上查看。帮我在页面内制作一些“搜索框”。搜索框将只搜索和查看数据库表中较近的关键字 示例:当我搜索“Jenny”时,搜索框的结果将是数据库表中的所有“Jenny”。当我的搜索框找不到“Jenny”时,搜索框会显示“找不到结果” 这是我在customers.php中的代码如何使用PHP和Mysql将搜索框放入数据库表中,php,mysql,search,mysqli,Php,Mysql,Search,Mysqli,我已经在数据库中创建了“客户信息”。我还把它放在customers.php页面上查看。帮我在页面内制作一些“搜索框”。搜索框将只搜索和查看数据库表中较近的关键字 示例:当我搜索“Jenny”时,搜索框的结果将是数据库表中的所有“Jenny”。当我的搜索框找不到“Jenny”时,搜索框会显示“找不到结果” 这是我在customers.php中的代码 <?php $conn = mysqli_connect('localhost','root','','newcartdb');
<?php
$conn = mysqli_connect('localhost','root','','newcartdb');
if(mysqli_connect_errno()){
echo 'Failed to Connect: '.mysqli_connect_error();
}
if(isset($_POST['delete'])){
$DeleteQuery = "DELETE FROM customers WHERE id='$_POST[hidden]'";
mysqli_query($conn,$DeleteQuery);
}
$query = "SELECT * FROM customers ORDER BY id ASC";
$results = mysqli_query($conn,$query);
echo '<table class="table table-bordered">
<thead>
<tr>
<th width="40px">ID</th>
<th>Email</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Gender</th>
<th>Titlename</th>
<th>BirthMonth</th>
<th>BirthDay</th>
<th>BirthYear</th>
<th>Cellphone Number</th>
<th>Phone Number</th>
<th>Address1</th>
<th>Address2</th>
<th></th>
</tr>
</thead>
<tbody>
</tbody>';
while($userData = mysqli_fetch_array($results)){
echo '<form action="customers.php" method="POST">';
echo '<tr>';
echo '<td>'.$userData['id'].'</td>';
echo '<td>'.$userData['Email'].'</td>';
echo '<td>'.$userData['Firstname'].'</td>';
echo '<td>'.$userData['Lastname'].'</td>';
echo '<td>'.$userData['Gender'].'</td>';
echo '<td>'.$userData['Titlename'].'</td>';
echo '<td>'.$userData['BirthMonth'].'</td>';
echo '<td>'.$userData['BirthDay'].'</td>';
echo '<td>'.$userData['BirthYear'].'</td>';
echo '<td>'.$userData['CellphoneNumber'].'</td>';
echo '<td>'.$userData['PhoneNumber'].'</td>';
echo '<td>'.$userData['Address1'].'</td>';
echo '<td>'.$userData['Address2'].'</td>';
echo '<td><input type="hidden" name="hidden" value="'.$userData['id'].'">';
echo '<td><input type="submit" name="delete" value="Delete" class="btn btn-info" / ></td>';
echo '</tr>';
echo '</form>';
}
echo '</table>';
?>
要做到这一点,您需要知道一些事情。首先是安全位 基本上,您不希望信任提交到应用程序的数据。在接受MySQL语句中使用的数据时,可以使用PHP针对MySQL的内置转义函数。 参考: 您似乎已经知道如何从提交的表单中获取数据,因为我看到您访问了$\u POST superglobal。对于您的搜索,您将执行类似的操作。它类似于$_POST['search'],您可以从一个表单中获取数据,表单中有一个名为“search”的文本元素 您将需要一个SQL查询来搜索结果。在没有看到您的数据库模式的情况下,很难对用户说,但我怀疑这会起作用
$search = mysqli_real_escape_string($_POST['search']);
$query= "
SELECT *
FROM customers
WHERE
Firstname LIKE '%{$search}%'
OR Lastname LIKE '%{$search}%'
OR Email LIKE '%{$search}%'
";
$results = mysqli_query($conn,$query);
一旦你有了结果,你应该能够像你给出的例子一样显示它们
希望这有帮助 你有SQL注入和XSS漏洞。哇,你正在慢慢地构建你的项目,所以哈哈,阅读,更新,删除。无论如何,创建一个表单并查询一个
,比如,它仍然太宽泛了sir@SLaks我怎样才能使它在SQL注入中不受攻击呢?:)你需要学习如何使用参数。顺便说一下,你不必在每句话中都说“先生”。