给定一个MYSQL数据库,在该数据库中,我有一个表,其中包含许多结构如下的条目:

字符串类型

字段“类型”在数据库中可以有4个值(a,b,c,d)。

我想使用复选框形式通过检查四个可能值之一来从数据库检索所有字符串。

到目前为止,我只剩下这段PHP代码:

<?
$objConnect = mysql_connect("localhost","root","****") or die("No DB to select.");
$objDB = mysql_select_db("exercises");
$strSQL = "SELECT * FROM entries WHERE type = '".$_POST["type"]."'";
$objQuery = mysql_query($strSQL);
?>

<?
$checkbox = array();

if (isset($_POST["type"])){
 $checked = $_POST["type"];

foreach ($checked as $value) {
    echo "$value"."</br>";
}
}
else{
    echo "Please select at least one type.";
    }
?>


问题在于此代码仅返回类型值a,b,c,d,而不返回数据库中条目的字符串。

有人可以告诉我如何实际访问我的数据库条目并检索与表单中已检查类型相对应的字符串值吗?
谢谢!

PS:这是我要使用的HTML格式:

<html>
 <head>
  <title>select</title>
  <META http-equiv="Content-Type" content="text/html; charset=latin-1">
 </head>
 <body bgcolor="#F5FAE6">
<center>
<h2><p align="center">Make your test</p></h2>
</center>

<br><br>

<form action="output.php" method="POST">
&#10004;&nbsp;Select the <b>type(s) of exercise</b> you need:<br /><br />

<table border="1" cellpadding='4' cellspacing='4' style='border-collapse: collapse' bordercolor='#9999DD'>
<tr><td><input type="checkbox" name="type[]" value="abc"/> multiple choice</td></tr>
<tr><td><input type="checkbox" name="type[]" value="error"/> mistake correction</td></tr>
<tr><td><input type="checkbox" name="type[]" value="cloze"/> cloze</td></tr>
<tr><td><input type="checkbox" name="type[]" value="makeq"/> make a question</td></tr>
<tr><td><input type="checkbox" name="type[]" value="trans"/> translate (IT-->EN)</td></tr>
</table>

<p align="center">
<input type="submit" name= "get" value="get your entries!"/>
</p>

</form>

</body>
</html>

最佳答案

问题是您正在回显类型,而不是从mysql查询返回的结果。

您的代码应为:

if (isset($_POST["type"])){
 while ($row = mysql_fetch_array($objQuery)) {
    echo $row['text']."<br />";
}
else{
    echo "Please select at least one type.";
}

关于php - PHP复选框以检索数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17769837/

10-14 17:04
查看更多