php搜索字段无效


php Search field not working

我在这个网站上和网上的其他网站上看到了一个非常相似、非常相似的。然而,在多个论坛和youtube上呆了三天后,我的脚本似乎无法正常工作。

我建立了一个简单的页面,可以跟踪我的车辆里程,我希望能够按位置搜索表格。除了搜索功能外,一切都很好。该表显示在页面上,其中填充了sql表中的所有行。但是,当我键入搜索字段并单击提交时,不会显示搜索结果。什么都没发生。

为了帮助您,我将发布我的代码以及我的服务器、php版本和mysql版本。

phpMyAdmin:4.0.10.7

数据库服务器:

服务器:通过UNIX套接字的本地主机

服务器类型:MySQL

服务器版本:5.5.46-cll-MySQL社区服务器(GPL)

协议版本:10

服务器字符集:UTF-8 Unicode(utf8)

Web服务器:

cpsrvd 11.52.1.3

数据库客户端版本:libmysql-5.1.73

PHP扩展:mysqli

<?php
session_start();
if(empty($_SESSION['user']))
{
echo "      <script>window.top.location='https://mysitehere.com/milelog/login/login2.php'</script>";
exit;
}
?>
<?php
include_once 'carselect/function.php';
connect()
?>
<?php
//The File for the Database Connection
include('editentry/con.php');
//The SQL Query
$table = "SELECT * FROM milelog ";
If($_POST){
$input = mysql_real_escape_string($_POST['location']);
$table .= "WHERE Location = '$input'";
}

$show = mysql_query($table) or die(mysql_error());

?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/milestyle.css">
</head>
<body class="body">
<div style="border: 1px solid; background-color: silver;"><div class="menuwrap"><ul class="sonarmenu" style="padding: 0 !important;">
<li><a href="addentry.php">Log</a></li>
<li><a href="addcar.php">Add Car</a></li>
<li><a href="list.php">Edit Entry</a></li>
<li><a href="login/logout.php">Log Out</a></li>
</ul>
</div></div>
<form name="search" action="search.php" method="post">
<input name="location" value="" type="text" />
<input type="submit" value="search" name"search" />
</form>
<table class="table">
<tr>
<td>ID</td>
<td>Car</td>
<td>Date</td>
<td>Location</td>
<td>Miles</td>
<td></td>
<td></td>
</tr> 
<?php while ($row = mysql_fetch_array($show)) {
echo "<tr><td>".$row['id']."</td>";
echo "<td>".$row['Car']."</td>";
echo "<td>".$row['Date']."</td>";
echo "<td>".$row['Location']."</td>";
echo "<td>".$row['Miles']."</td>";
echo "<td><a href='editentry/delete2.php?id=".$row['id']."'>Delete</a></td>    <tr>";
 }
?>
</table>

</body>
</html>

经过三天漫长的研究,我不知道还能做什么。

您使用的是mysql_real_escape_string,它在PHP 5.5.0中被弃用,在7.0.0中被删除。下面的改变会起作用,但这不是一个真正的解决方案:

更改

$input = mysql_real_escape_string($_POST['location']);

$input = $_POST['location'];

这仍然会使您(甚至更容易)受到SQL注入的攻击,因此您应该真正使用准备好的语句。有关完整示例,请参阅手册。

请注意,如@self所示,测试if($_POST)不是检查是否发生post的正确方法。一种正确的方法是这样(详细信息请参阅此答案):

if ($_SERVER['REQUEST_METHOD'] == 'POST')