我想添加一个搜索框,使用%like%
搜索内容。
我已经尝试了太多,但得到的错误数量。
我已经应用了这样的查询:
<?php
//including the database connection file
include_once("config.php");
//fetching data in descending order (lastest entry first)
$result = mysql_query("SELECT * FROM users ORDER BY id DESC");
?>
<html>
<head>
<title>Homepage</title>
</head>
<body>
<a href="add.html">Add New Data</a><br/><br/>
<table width='80%' border=0>
<tr bgcolor='#CCCCCC'>
<td>Name</td>
<td>Age</td>
<td>Email</td>
<td>Update</td>
</tr>
<?php
while($res = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>".$res['name']."</td>";
echo "<td>".$res['age']."</td>";
echo "<td>".$res['email']."</td>";
echo "<td><a href=\"edit.php?id=$res[id]\">Edit</a> | <a href=\"delete.php?id=$res[id]\" onClick=\"return confirm('Are you sure you want to delete?')\">Delete</a></td>";
}
?>
</table>
</body>
</html>
答案 0 :(得分:4)
<form method="POST">
<input name="search" type="text">
<button type="submit">Search</button>
</form>
<?php
if($_POST){
$data = $_POST['search'];
$q = mysql_query("SELECT * FROM `users` LIKE '%$data%' ORDER BY id DESC");
while($res = mysql_fetch_array($q)) {
echo "<tr>";
echo "<td>".$res['name']."</td>";
echo "<td>".$res['age']."</td>";
echo "<td>".$res['email']."</td>";
echo "<td><a href=\"edit.php?id=$res[id]\">Edit</a> | <a href=\"delete.php?id=$res[id]\" onClick=\"return confirm('Are you sure you want to delete?')\">Delete</a></td>";
echo "</tr>";
}
}
?>
P.S。不推荐使用mysql,使用mysqli和良好的转义或mysqli预处理语句或PDO