我有这样的代码:搜索查询将返回一个空的结果集
PHP:
[CONNECT TO DATABASE OR DIE (THIS WORKS)]
if(isset($_GET['search'])) // If it's submitted
{
$inp = Clean($_GET['inpname']); // Clean my input
$sQuery="SELECT name FROM user WHERE (id LIKE '%$inp%')"; // mySql query
$r = mysql_query($sQuery) or die(mysql_error());
if(mysql_affected_rows()===0) // If no match found
echo "{$inp} is not in our database.";
else
{
echo "<p>{$inp} was successfully searched.</p>"; // Yes, the query worked
while($row=mysql_fetch_array($r)) // Loop through the query results
echo "{$row[0]}<br>"; // Show the results
} // End of the else statement
} // End of the if statement
function Clean($str) // Clean my input
{
return mysql_real_escape_string(strip_tags(trim($sStr))); // Remove traces of injection
}
HTML:
<form name="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
<input name="inpname" type="text">
<input type="submit" name="search" value="Search">
</form>
在我的数据库我想搜索的成员由ID,但是当我搜索例如1我没有结果:不在我们的数据库中。 (/index.php?inpname=1 &搜索=搜索)
如果我使用'%'。$ inp。“%'而不是'%$ inp%',这是行得通的,但它显示我的数据库中的所有用户。
什么是从'预期$ _GET ['inpname']'一个int或一个字符串,例如一个名字,你的脚本也容易受到xss和PDO的攻击,或者mysqli应该被用于mysql_ * fun ctions。 – 2012-07-19 07:18:40
任何建议使用什么? – 2012-07-19 07:24:01
你的干净的函数有一个错误'修剪($ sStr)' - ** $ sStr **当它应该只是'$ str' – 2012-07-19 07:27:50