【发布时间】:2013-11-21 16:47:17
【问题描述】:
我希望能够单击一个复选框,然后从我的数据库中的表中返回结果。我认为我的一切都是正确的,除了我的查询以提取数据并以表格格式显示结果。
This is my index.php file that makes the checkboxes.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Search</title>
</head>
<body>
<form action="filter.php" method="post">
<INPUT TYPE=CHECKBOX NAME="option[]" VALUE="PhoneName" id="r1">PhoneName
<INPUT TYPE=CHECKBOX NAME="option[]" VALUE="8GB" id="b1">8GB
<INPUT TYPE=CHECKBOX NAME="option[]" VALUE="16GB" id="g1">16GB
<INPUT TYPE=CHECKBOX NAME="option[]" VALUE="Android" id="g1">Android
<INPUT TYPE=CHECKBOX NAME="option[]" VALUE="Apple" id="g1">Apple
<input type="submit" name="formSubmit" value="Filter" />
</form>
</body>
</html>
这是我的 filter.php,PHP 代码开始的地方
<?php
mysql_connect("localhost", "root", "") or die (mysql_error());
mysql_select_db("James") or die (mysql_error());
$sql = 'SELECT * FROM hardware';
$query=array();
if (isset($_POST['option'])){
foreach ($_POST['option'] as $opt){
switch ($opt){
case 'PhoneName': $query[] = "PhoneName='yes'";
case '8GB': $query[] = "8GB='yes'";
case '16GB': $query[] = "16GB='yes'";
case 'Android': $query[] = "Android='yes'";
case 'Apple': $query[] = "Apple='yes'";
}
}
}
if (count($query)>0){
$sql .= 'WHERE '.implode(' IN ', $query);
print_r($_POST['option']);
}
任何帮助将不胜感激,因为我花了几天时间编写各种不同的代码,但这是我现在所能得到的。我几天前才开始学习php,所以我意识到它可能不太好
【问题讨论】:
-
id 对于所有复选框应该是唯一的