Phatter
Phatter

Reputation: 23

Search MySQL with Multiple Fields in a Form

I've a problem to asking . When I've input value to searching fromreport_id field that always happened Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in . All fields can search but report_id field can't search , I don't know how to fix it

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>

Upvotes: 1

Views: 701

Answers (1)

Asaph
Asaph

Reputation: 162771

The most likely cause of Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in is that your call to mysql_connect returned FALSE because it was unable to connect to your database. From the docs:

Returns a MySQL link identifier on success or FALSE on failure.

Upvotes: 1

Related Questions