【发布时间】:2018-08-19 18:18:48
【问题描述】:
我是 PHP 和 MySQL 的新手,我很难根据包含多个字段的 html 搜索表单从数据库中提取数据。 1st 字段(*必填)= 带选项选择(选择名称 = area_name),2nd = 输入类型:学校名称,3rd =日期,4-5 日 = time_from,time_to。问题:如何根据上述字段提取讲师姓名?
以下是 PHP 代码(忽略 db connect,它正在工作):
if (!empty($_REQUEST['term'])) {
$term = mysql_real_escape_string($_REQUEST['term']);
(line 8)
$sql = mysqli_query("SELECT lecturer_name, city, phone, e-mail * FROM
area_name where area_name LIKE '$search'") UNION ("SELECT * FROM school
where school_name LIKE '$search'") UNION ("SELECT * FROM schedule where
date LIKE '$search'") UNION ("SELECT * FROM schedule where time_from
LIKE '$search'") UNION ("SELECT * FROM schedule where time_to LIKE
'$search'");
$r_query = mysqli_query($sql);
echo "<table border='1' cellpadding='5'>";
echo "<tr> <th>Lecturer Name</th> <th>City</th> <th>Phone</th>
<th>Email</th> <th></th> <th></th></tr>";
// loop through results of database query, displaying them in the table
while ($row = mysql_fetch_array($r_query)){
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['lecturer_name'] . '</td>';
echo '<td>' . $row['city'] . '</td>';
echo '<td>' . $row['phone'] . '</td>';
echo '<td>' . $row['email'] . '</td>';
echo "</tr>";
}
// close table>
echo "</table>";
}
$conn->close();
结果出现如下错误:
解析错误:语法错误,意外的 'UNION' (T_STRING) 在 D:\XAMPP\htdocs\trv\search_lecturer.php 在第 8 行
我不知道如何将 html 字段名称与 mysql 连接。非常感谢任何帮助!
【问题讨论】:
-
为什么
SELECT lecturer_name, city, phone, e-mail *中有*? -
我只需要提取这些字段
-
星号表示全选...
-
即使我只输入 SELECT * ... 它也会显示错误:(
-
你应该分开你的任务来了解这些事情是如何完成的。您正在尝试同时学习 PHP 和 MySQL。首先在命令行上学习 MySQL,学习如何构建查询。当你有了它,继续使用 PHP。