我想問一下這段代碼。我有兩個下拉菜單和一個按鈕。我想在sql數據庫中搜索我在這些下拉菜單中選擇的內容。使用兩個下拉菜單,sql數據庫中的搜索項的sql語法是什麼?兩個下拉菜單和一個按鈕
我的數據庫=測試
表=學生
name | class | sex | mark |
John | Five | Male | 75
Jashi | Four | Female | 89 |
##HTML##
<form action="search2.php" method="post">
<select name="class">
<option value="" selected="selected">Class</option>
</select>
<select name="sex">
<option value="" selected="selected">Sex</option>
</select>
<input type="submit" value="search" />
</form>
search2.php
<?php
mysql_connect('localhost', 'root', '');
mysql_select_db ("test");
$whereClauses = '';
$class = count($_POST['class']);
$sex = count($_POST['sex']);
$i = 0;
if (! empty($_POST['class'])) {
foreach ($_POST['class'] as $class) {
$whereClauses .="class='".mysql_real_escape_string($class)."'";
if ($i++ == $class) {
$whereClauses .= " AND";
}
}
}
if (! empty($_POST['sex'])) {
foreach ($_POST['sex'] as $sex) {
$whereClauses .="sex='".mysql_real_escape_string($sex)."'";
}
if ($i++ == $sex) {
$whereClauses .= " AND";
}
}
$sql = "SELECT * FROM student '".$where."' ORDER BY id DESC '".$limit."'";
$result=mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo $row['class'];
echo $row['sex'];
echo $row['mark'];
}
?>
ANY HELP WOULD BE APPRECIATED
發佈您的代碼。你想要什麼輸出。 –
代碼在哪裏? –