我觉得我只需要再多看一眼。数据库中当然有要搜索的内容,但没有显示任何内容。语法或逻辑有问题吗。这都在一个文件index.php 中
<form action = "index.php" method = "post">
Search: <input type="text" name="value" placeholder="Is it part of the FWO?"></input>
<input type=submit name = "search" value="Search">
</form>
<a href="LINKY">New Entry</a>
<br>
<p>Search Results</p>
<hr />
<?php
error_reporting(E_ALL);
$title = $_POST['value'];
echo "You have searched: " .$title;
echo "<br>";
$con = mysql_connect("localhost", "user", "pass") or die ('Could not connect, this is the error: ' . mysql_error());
mysql_select_db("db") or die ('Sorry could not access database at this time. This is the error: ' . mysql_error());
$clean = msql_real_escape_string($_GET['value']);
echo "Another test ". $clean;
$run = mysql_query("SELECT * FROM db WHERE name = '$clean'") or die(mysql_error());
if(mysql_num_rows($run) >= 1){
echo "found entry";
while($i = mysql_fetch_array($run)){
echo $i['creator'];
}
}
else {
echo "No entries found";
}
mysql_close($con);
?>
</body>
</html>
您的表单正在使用post方法,并且您正在尝试通过$_get 获取值
而不是这个:
$clean = msql_real_escape_string($_GET['value']);
使用此:
$clean = msql_real_escape_string($_POST['value']);
或
$clean = msql_real_escape_string($title);
要在mysql中搜索,应该使用LIKE。如果你想搜索字符串中的任何地方,你应该用%封装。例如:
$run = mysql_query("SELECT * FROM db WHERE name LIKE '%$clean%'") or die(mysql_error());
了解更多信息:http://dev.mysql.com/doc/refman/5.7/en/string-comparison-functions.html