美好的一天。所以我有一个程序将从MySql中获取一组行,具体取决于我在HTML中的select选项中单击的select选项,并将其显示在HTML中的容器中。代码适用于第一个,但不知何故,在第二个代码,它不会获取我请求的行。我已经检查了列名是否正确以及select选项的值是否与我的PHP代码中的“if语句”匹配。
在第一段代码中,它可以正常运行,
if($_POST["FilterDoc"]=="document_type")
{
$result=mysqli_query($conn,"SELECT * FROM records ORDER BY document_type ASC");
$data = "";
while($rows = mysqli_fetch_assoc($result)){
$document_type = $rows['document_type'];
$date_received = $rows['date_received'];
$application_no = $rows['application_no'];
$hei = $rows['hei'];
$school_name = $rows['school_name'];
$from_co = $rows['from_co'];
$other_govt = $rows['other_govt'];
$contact_person = $rows['contact_person'];
$comment = $rows['comment'];
$program = $rows['program'];
$year_level = $rows['year_level'];
$academic_year = $rows['academic_year'];
$transaction_no = $rows['transaction_no'];
$output = "<table><tr><td>$document_type</td><td>$date_received</td><td>$application_no</td><td>$hei</td><td>$school_name</td><td>$from_co</td><td>$other_govt</td><td>$contact_person</td><td>$comment</td><td>$program</td><td>$year_level</td><td>$academic_year</td><td>$transaction_no</td></tr></table><br>";
$data .= '<a href = "editdoc.php?v='.$transaction_no.'" name="documents">'.$output.'</a>';
}
echo $data ;
}
但是在第二段代码中,即使它具有相同的代码结构,也不像我希望的那样工作,
else if($_POST["FilterDoc"]=="other")
{
$result=mysqli_query($conn,"SELECT * FROM records ORDER BY other_govt ASC");
while($data = mysql_fetch_assoc($result))
$data = "";
while($rows = mysqli_fetch_assoc($result)){
$document_type = $rows['document_type'];
$date_received = $rows['date_received'];
$application_no = $rows['application_no'];
$hei = $rows['hei'];
$school_name = $rows['school_name'];
$from_co = $rows['from_co'];
$other_govt = $rows['other_govt'];
$contact_person = $rows['contact_person'];
$comment = $rows['comment'];
$program = $rows['program'];
$year_level = $rows['year_level'];
$academic_year = $rows['academic_year'];
$transaction_no = $rows['transaction_no'];
$output = "<table><tr><td>$other_govt</td><td>$document_type</td><td>$date_received</td><td>$application_no </td><td>$hei</td><td>$school_name</td><td>$from_co</td><td>$contact_person</td><td>$comment</td><td>$program</td><td>$year_level</td><td>$academic_year</td><td>$transaction_no</td></tr></table><br>";
$data .= '<a href = "editdoc.php?v='.$transaction_no.'" name="documents">'.$output.'</a>';
}
echo $data;
}
这是我的选择选项
的HTML代码<select name = "FilterDoc" onchange = "filterby(this);">
<option selected disabled>Filter By</option>
<option value="document_type">Document Type</option>
<option value="date">Date</option>
<option value="hei">HEI</option>
<option value="other">Other Govt.</option>
<option value="person">Person</option></select>
我的数据库中 other_govt 的列名不是主键,并且具有重复值。我将衷心感谢您的帮助。谢谢!
答案 0 :(得分:0)
您的所有功能代码都很好看。但是,你已经打电话了
while($data = mysql_fetch_assoc($result))
在第二个代码中。
我认为 mysql_fetch_assoc 无法再调用两次。
删除不需要的行并进行检查。
答案 1 :(得分:0)
从MySQL CLI或PHPMyAdmin运行查询SELECT * FROM records ORDER BY other_govt ASC
以确保其有效。检查mysqli_query()
的错误代码。
此循环以静默方式删除所有结果行:
while($data = mysql_fetch_assoc($result))
$data = "";
这是一个错误。
您可能会发现使用extract()
函数将所有$row
值拉入局部变量会更容易。或者使用mysqli_fetch_ojbect($result)
然后使用...<td>{$rows->document}</td>...
生成输出。
通常一个好主意是使用htmlspecialchars()
转义输出到HTML的字符串。假设关联数组:
您的代码将为每一行生成一个表,可能不希望您想要。将<table>
标记放在循环之外,将<tr>
放在每行的循环中,然后可以迭代列值:
foreach ($rows as $col => $val) {
$output .= '<td>'.htmlspecialchars($val).'</td>';
}
您可以在SELECT
中指定列,而不是使用SELECT *
,以表格的顺序获取列。
答案 2 :(得分:0)
您的查询后面还有一行额外的代码:
while($data = mysql_fetch_assoc($result))
答案 3 :(得分:0)
尝试以下
$mQuery = "";
$Filter = $_POST["FilterDoc"];switch ($Filter) {
case "document_type":
$mQuery = "your query for document type";
break; case "other":
$mQuery = "your query for other type";
break; } if (!empty($mQuery)) { {
$result = mysqli_query($conn, $mQuery);
$data = "";
while ($rows = mysqli_fetch_assoc($result)) {
$document_type = $rows['document_type'];
$date_received = $rows['date_received'];
$application_no = $rows['application_no'];
$hei = $rows['hei'];
$school_name = $rows['school_name'];
$from_co = $rows['from_co'];
$other_govt = $rows['other_govt'];
$contact_person = $rows['contact_person'];
$comment = $rows['comment'];
$program = $rows['program'];
$year_level = $rows['year_level'];
$academic_year = $rows['academic_year'];
$transaction_no = $rows['transaction_no'];
$output = "<table><tr><td>$document_type</td><td>$date_received</td><td>$application_no</td><td>$hei</td><td>$school_name</td><td>$from_co</td><td>$other_govt</td><td>$contact_person</td><td>$comment</td><td>$program</td><td>$year_level</td><td>$academic_year</td><td>$transaction_no</td></tr></table><br />";
$data.= '<a href = "editdoc.php?v=' . $transaction_no . '" name="documents">' . $output . '</a>';
}
echo $data;
}
else {
echo "Unknown Filter Request";
}