【问题标题】:html search form linked with MySQL db [duplicate]与MySQL db链接的html搜索表单[重复]
【发布时间】: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。

标签: php html mysql sql


【解决方案1】:

UNION 是 SQL 运算符,而不是 PHP。您的 PHP 语法有错误。

$sql = mysqli_query("(SELECT .....) UNION (SELECT .....) UNION .....");

附: * 表示所有字段,如果指定字段,则 * 不是必需的。

【讨论】:

  • 托尔您好,谢谢!如果我需要从不同的mysql表中选择相同的数据怎么办?前任。 $sql = mysqli_query("(选择讲师姓名、城市、电话、电子邮件来自区域 WHERE area_name LIKE '$search') UNION (选择讲师姓名、城市、电话、电子邮件来自学校 WHERE school_name LIKE '$search') UNION .... .");
  • 我已经搜索过类似的主题,但是当我需要没有在 html 搜索字段中显示的信息时找不到我的案例,它只是与之相关。 (需要地区、学校和日程表中的讲师数据)
  • 如果你放了一个php变量,我建议把它分开。 mysqli_query(“(选择讲师姓名,城市,电话,电子邮件来自区域WHERE area_name LIKE'”。$search。“')联合......其他一切都是正确的。
  • 没有。使用带有占位符的准备好的语句。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-25
  • 2015-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-02
  • 1970-01-01
相关资源
最近更新 更多