我是PHP和MySQL的新手,很难根据包含多个字段的html搜索表单从db中提取数据。 第一个字段(*必填)=选择选项(选择名称= area_name
),第二 =输入类型:学校名称,第三 = date,4-5th = 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();
在结果中,出现以下错误:
解析错误:语法错误,意外&#39; UNION&#39; (T_STRING)in 第8行的D:\ XAMPP \ htdocs \ trv \ search_lecturer.php
我无法获得如何将html字段名称与mysql连接。非常感谢任何帮助!
答案 0 :(得分:0)
UNION是SQL运算符,而不是PHP。您在PHP语法中有错误。
$sql = mysqli_query("(SELECT .....) UNION (SELECT .....) UNION .....");
P.S。 *表示所有字段,如果指定字段,则* isn不需要。