php - Search MySQL with Multiple Fields in a Form -
i've problem asking . when i've input value searching fromreport_id
field happened warning: mysql_fetch_array() expects parameter 1 resource, boolean given in
. fields can search report_id
field can't search , don't know how fix it
if(isset($_post['submit'])) { // define list of fields $fields = array('report_name', 'report_id', 'abstract', 'student_name', 'teacher_name'); $conditions = array(); foreach($fields $field){ // if field set , not empty if(isset($_post[$field]) && $_post[$field] != '') { // create new condition while escaping value inputed user (sql injection) $conditions[] = "`$field` '%" . 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 student_name asc separator ', ') student_name student right join report on student.report_id = report.report_id group report_name order report_name asc"; // if there conditions defined if(count($conditions) > 0) { // append conditions $query = "select report.report_id, report_year, report_status, report_type, teacher_name, abstract, report_position, report_name, group_concat(student_name order student_name asc separator ', ') student_name student right join report on student.report_id = report.report_id " . implode (' , ', $conditions) . " group report_name order 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 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>
the cause of warning: mysql_fetch_array() expects parameter 1 resource, boolean given in
call mysql_connect
returned false
because unable connect database. docs:
returns mysql link identifier on success or false on failure.
Comments
Post a Comment