你好,我在这项任务中遇到了很大的问题
我在xampp上建立了一个名为search_test的数据库,其中有名字和姓氏作为字段。我已经设置了一个php表单,这样当用户输入一个名字时,比如说Andre,它会返回数据库中的所有andres。有一个问题,它一直告诉我没有搜索结果,即使我知道数据库里有数据,这里的代码应该是一个叫做index.php的php页面
    <?php
    mysql_connect("localhost","michael","xcA123sd") or die(mysql_error());
    mysql_select_db("search_test") or die ("could not find db"); 
     $output ='';
    if (isset ($_POST['search']));
        $searchq = $_POST['search'];
    $query = mysql_query("SELECT * FROM members WHERE firstname LIKE '%searchq%'" ) or die("could not search");
    $count = mysql_num_rows($query);
    if($count == 0){
        $output = 'There was no search results !';
        }else{
        while($row = mysql_fetch_array($query)){
        $fname = $row['firstname'];
        $output .='<div> '.$fname.'</div>';
        }
        }
}
?>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>search</title>
</head>
<body>
<form action="index.php" method="post">
<input type="text" name="search" placeholder="search for members"/> 
<input type="submit" value=">>"/>
</form>
<?php print("$output);?>
</body
</html> 例如,我键入andre in,就会得到响应
没有搜索结果!
有没有人能帮帮忙
发布于 2013-04-01 00:18:29
问题来自您代码的这一行
$query = mysql_query("SELECT * FROM members WHERE firstname LIKE '%searchq%'“)
变量searchq后面没有$
发布于 2013-04-01 00:22:22
首先:你想
$query = mysql_query("SELECT * FROM members WHERE firstname LIKE '%searchq%'" ) or die("could not search");成为
$query = mysql_query("SELECT * FROM members WHERE firstname LIKE '%$searchq%'" ) or die("could not search");(注意附加的$)。
也就是说,您有一个很大的SQL注入问题:假设我“正常”运行了一次查询:这让我对列有了一个概念。现在我发布' UNION ALL SELECT correct_field_num FROM information_schema.TABLES WHERE NAME LIKE '%作为我的搜索--这给了我你的表结构。使用posting ' UNION ALL SELECT correct_column_num FROM any_table_name WHERE 'x' LIKE '%,我可以读取任意表。
请确保您使用了一种广为人知的技术来构造来自任何用户输入的安全查询。从不推荐使用的mysql_real_escape_string()到参数化查询都有不同程度的变化。
发布于 2013-04-01 00:13:43
LIKE '%searchq%'"它会搜索像'searchq‘这样的字符串。如果您需要作为变量,请添加相应的美元符号
https://stackoverflow.com/questions/15731470
复制相似问题