【发布时间】:2017-06-22 08:42:54
【问题描述】:
我想使用多个输入字段在我的数据库中搜索,但它给了我一个错误。
错误消息:“您的 SQL 语法有错误;请查看与您的 MariaDB 服务器版本相对应的手册,了解在第 1 行的 '' 附近使用的正确语法”
如您所见,我在各个列中有 3 个输入字段可供搜索。 并非所有字段都是强制性的。用户只能搜索 1 或 2 个字段。
场景; 1.当我在第一个字段中输入任何字符串时,它将在数据库中搜索。
当我在第一个和第二个字段中输入任何字符串时,它会再次搜索。
现在,我只在第二个字段中输入了任何字符串。然后错误来了。
请帮助大家。
提前谢谢.d
<html>
<body>
<form name="form" action="index1.php" method="get">
Profile Number: <input id="profile_number" name="profile_number">
Nice Name: <input id="nice_name" name="nice_name">
ISO 3: <input id="iso_3" name="iso_3">
<input type="submit" name="Submit" value="Search" />
</form>
<?php
echo @$_GET['profile_number'];
echo @$_GET['nice_name'];
echo @$_GET['iso_3'];
$conn = mysqli_connect("localhost","root","","autocomplete");
if (mysqli_connect_errno()) {
echo "Failed to connect: " . mysqli_connect_error();
}
if(isset($_GET['Submit']))
{
echo'
<table width=100% style="border: 1px solid #a8c562;" cellpadding=3><tr>
<td bgcolor=#cadd99 align=center style="border: 1px solid #a8c562;">Profile Number</td>
<td bgcolor=#cadd99 align=center style="border: 1px solid #a8c562;">Country Name</td>
<td bgcolor=#cadd99 align=center style="border: 1px solid #a8c562;">ISO3</td>
';
$usearchprofile = @$_GET['profile_number'];
$usearchnicename = @$_GET['nice_name'];
$usearchiso3 = @$_GET['iso_3'];
$qrystring = "SELECT * FROM country where ";
if($usearchprofile)
$qrystring .= " name like '%$usearchprofile%' ";
if($usearchnicename)
$qrystring .= " OR nicename like '%$usearchnicename%' ";
if($usearchiso3)
$qrystring .= " OR iso3 like '%$usearchiso3%' ";
$userarray = mysqli_query($conn, $qrystring) or die(mysqli_error($conn));
while ($usrow = mysqli_fetch_array($userarray))
{
echo'
<tr>
<td style="border: 1px solid #a8c562;" align=center>'.$usrow['name'].'</td>
<td style="border: 1px solid #a8c562;" width=10% align=center>'.$usrow['nicename'].'</td>
<td style="border: 1px solid #a8c562;" width=10% align=center>'.$usrow['iso3'].'</td>
</tr>
';
}
echo'</table>';
}?>
</body>
</html>
【问题讨论】:
-
您应该尝试打印您的最终查询以查看它的作用。我很确定你会得到你的 sql 查询的错误。一些建议: - 如果可以的话,使用 PDO 而不是 mysqli 函数 - 我更喜欢使用语法 if(isset($_GET["iso_3"])) 但这取决于你 - 你可以使用替代语法:php.net/manual/en/control-structures.alternative-syntax.php