我需要一些关于我的代码的帮助。到目前为止,当我转到页面时,可以看到整个视图。当我设置搜索并发布它时,它也可以工作。但是当我添加 --> 2 个或更多 <-- 关键字时,我得到一个错误。所以问题出在 forarch 循环 else 构造中。
在这种情况下,我收到此错误:
查询 fout 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 6 行的“ORDER BY custsurname、custforename、custmidname”附近使用正确的语法
我尝试了许多构建方法,但不知何故似乎不起作用。
我的代码如下:
if(isset($_POST['search']))
{
$search = $_POST['search'];
$terms = explode(" ", $search);
$customerlistquery = "
SELECT *
FROM customer
LEFT JOIN company
ON customer.compid=company.compid
WHERE
";
foreach ($terms as $each)
{
$i++;
if ($i == 1)
{
$customerlistquery .= "concat(custsurname, custforename, custmidname) LIKE '%$each%' ORDER BY custsurname, custforename, custmidname";
}
else
{
$customerlistquery .= "OR concat(custsurname, custforename, custmidname) LIKE '%$each%' ORDER BY custsurname, custforename, custmidname";
}
}
}
else
{
$customerlistquery = "
SELECT *
FROM customer
LEFT JOIN company
ON customer.compid=company.compid
ORDER BY custsurname, custforename, custmidname
";
}
还有最后一个问题。为什么 PHP 抱怨 undefined 变量:i?
Notice: Undefined variable: i in ...
这是每个“一次性变量”的标准消息吗?