我是 PHP 和 MySQL 的新手,很难根据包含多个字段的 html 搜索表单从数据库中提取数据。 1st 字段(*必填)= 选择选项(选择名称 = area_name
),2nd = 输入类型:学校名称,3rd =日期,4-5 日 = time_from,time_to。问题:如何根据上述字段提取讲师姓名?
下面是 PHP 代码(忽略数据库连接,它正在工作):
<小时/> 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();
结果出现如下错误:
Parse error: syntax error, unexpected 'UNION' (T_STRING) in D:\XAMPP\htdocs\trv\search_lecturer.php on line 8
我不知道如何将 html 字段名称与 mysql 连接起来。非常感谢任何帮助!
最佳答案
UNION 是 SQL 运算符,而不是 PHP。您的 PHP 语法有错误。
$sql = mysqli_query("(SELECT .....) UNION (SELECT .....) UNION .....");
附注* 表示所有字段,如果指定字段,* 不是必需的。
关于php - 与 MySQL 数据库链接的 html 搜索表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49223200/