gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 23:58:23 25 4
gpt4 key购买 nike

我有问题要问。当我从 report_id 字段中输入值进行搜索时,总是会发生这种情况警告:mysql_fetch_array() 期望参数 1 为资源, bool 值在.所有字段都可以搜索但是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 的最可能原因是您对 mysql_connect 的调用返回 FALSE 因为它无法连接到您的数据库。来自docs :

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

关于php - 使用表单中的多个字段搜索 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30830163/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com