使用表单中的多个字段搜索MySQL

时间:2015-06-14 13:50:23

标签: php html mysql sql forms

我有一个问题要问。 当我从始终发生的report_id字段搜索值时输入值 Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in 。所有字段都可以搜索,但report_id字段无法搜索,我不知道如何解决它

if(isset($_POST['submit'])) {
              // define the list of fields
                  $fields = array('report_name', 'report_id', 'abstract', 'student_name', 'teacher_name');
                  $conditions = array();
                  foreach($fields as $field){
    // if the field is set and not empty
                    if(isset($_POST[$field]) && $_POST[$field] != '') {
        // create a new condition while escaping the value inputed by the user (SQL Injection)
                      $conditions[] = "`$field` LIKE '%" . mysql_real_escape_string($_POST[$field]) . "%'";
                    }
                  }
                  $query = "SELECT report.report_id, report_year, report_status, report_type, teacher_name, abstract, report_position, report_name,
                  GROUP_CONCAT(student_name ORDER BY student_name ASC SEPARATOR ', ') AS student_name
                  FROM student RIGHT JOIN report ON student.report_id = report.report_id GROUP BY report_name
                  ORDER BY report_name ASC";
// if there are conditions defined
                  if(count($conditions) > 0) {
    // append the conditions
                    $query = "SELECT report.report_id, report_year, report_status, report_type, teacher_name, abstract, report_position, report_name,
                    GROUP_CONCAT(student_name ORDER BY student_name ASC SEPARATOR ', ') AS student_name
                    FROM student RIGHT JOIN report ON student.report_id = report.report_id WHERE " . implode (' AND ', $conditions) . " GROUP BY report_name
                    ORDER BY report_id asc, report_name ASC";
                  }

                  $result = mysql_query($query);
                }

HTML

<form action="searching.php" method="post" >
    <p><label>Project Name</label></p>
    <input name="report_name" type="text" class="form-control" placeholder="Enter Report name" id="report_name">
    <p><label>Project ID</label></p>
    <input name="report_id" type="text" class="form-control" placeholder="Enter ID Report" id="report_id">
    <p><label>Keyword</label></p>
    <input name="abstract" type="text" class="form-control" placeholder="Enter Some Keyword" id="abstract">
    <p><label>Student Name</label></p>
    <input name="student_name" type="text" class="form-control" placeholder="Enter Student Name" id="student_name">
    <p><label>Advisor Name</label></p>
    <input name="teacher_name" type="text" class="form-control" placeholder="Enter Advisor Name" id="teacher_name">
    <br><button type="submit" name="submit" class="btn btn-primary" >Submit</button>

1 个答案:

答案 0 :(得分:1)

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in的最可能原因是您对mysql_connect的调用返回FALSE,因为它无法连接到您的数据库。来自docs

  

成功时返回MySQL链接标识符,失败时返回FALSE。