[英]Search MySQL with Multiple Fields in a Form
我有问题要问。 当我从总是发生的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>
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in
的最可能原因Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in
是您对mysql_connect
的调用返回FALSE
因为它无法连接到您的数据库。 从文档:
成功时返回 MySQL 链接标识符,失败时返回 FALSE。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.