我想问一下这段代码。我有两个下拉菜单和一个按钮。我想在sql数据库中搜索我在那些下拉菜单中选择的内容。使用两个下拉菜单,sql数据库中搜索项的sql语法是什么。
这是我的数据库=测试 这是我的表
表=学生
name | class | sex | mark |
John | Five | Male | 75
Jashi | Four | Female | 89 |
这是我的代码
----------
<?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['name'];
echo $row['class'];
echo $row['sex'];
echo $row['mark'];
}
?>
----------
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>
ANY HELP WOULD BE APPRECIATED
答案 0 :(得分:0)
你可以像这个例子那样简单易行
你有类似的东西
<form method=post>
<?php
// some fetching over here like
$id = $_GET['id']; // getting id just for example
$sql = mysql_query("SELECT FROM `somewhere` where `id`='$id'");
<select name="somename">
while($someid = mysql_fetch_array($sql))
{
?>
<option value="<?php echo $someid['id']; ?>"><?php echo $displayname; ?></option>
<?php } ?>
</select>
<input type="submit" name="sub" /></form>
<?php
if(isset($_POST['sub']))
{
$search_id = $_POST['somename']; // value from option
$search = mysql_query("SELECT * FROM `somewhere` WHERE `id`='$search_id'");
// or you can use LIKE //
// LIKE THIS //
/*
$search = mysql_query("SELECT * FROM `somewhere` WHERE `something` LIKE %SOMETHING%");
*/
// Just example result down here //
$result = mysql_fetch_array($search);
echo '<table cellpadding="10">
<tr>
<th>Name</th><th>SOMETHING</tH>
</tr>
<tr>
<Td>'.$result['name'].'</td><td>'.$result['something'].'</td>
</tr>
</table>';
}
?>
答案 1 :(得分:0)
mysql_connect('localhost', 'root', '');
mysql_select_db ("test");
$whereClauses = '';
if (! empty($_POST['class'])) {
foreach ($_POST['class'] as $class) {
$whereClauses[] .="class='".mysql_real_escape_string($class)."'";
}
}
if (! empty($_POST['sex'])) {
foreach ($_POST['sex'] as $sex) {
$whereClauses[] .="sex='".mysql_real_escape_string($sex)."'";
}
}
$where = !empty($whereClauses) ? implode(' and ', $whereClauses) : '';
$sql = "SELECT * FROM student '".$where."' ORDER BY id DESC '".$limit."'";
$result=mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo $row['name'];
echo $row['class'];
echo $row['sex'];
echo $row['mark'];
}
答案 2 :(得分:0)
您的解决方案就在这里,试试这个
Database Table Structure
CREATE TABLE IF NOT EXISTS `student` (
`name` varchar(255) NOT NULL,
`class` varchar(255) NOT NULL,
`sex` varchar(255) NOT NULL,
`mark` int(12) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Insert Record
INSERT INTO `student` (`name`, `class`, `sex`, `mark`) VALUES
('John', 'Five', 'Male', 75),
('Jashi', 'Four', 'Female', 89);
<form action="" method="post">
<select name="class">
<option value="">-SELECT CLASS-</option>
<option value="Four">Four</option>
<option value="Five">Five</option>
</select>
<select name="sex">
<option value="">-SELECT GENDER-</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
<input type="submit" name="search" value="search" />
</form>
<?php
mysql_connect('localhost', 'root', '');
mysql_select_db ("test");
extract($_POST);
if(isset($search))
{
if($class!='' || $sex!='')
{
$wh='';
if($class!='') $wh.=" AND class='".$class."'";
if($sex!='') $wh.=" AND sex='".$sex."'";
$qry=mysql_query("SELECT * FROM student where 1 ".$wh."");
$total_record=mysql_num_rows($qry);
if($total_record>0)
{
echo "<table border='1'>
<tr>
<td>Name</td>
<td>Class</td>
<td>Sex</td>
<td>Mark</td>
</tr>
";
while($row=mysql_fetch_array($qry))
{
echo "<tr>
<td>".$row['name']."</td>
<td>".$row['class']."</td>
<td>".$row['sex']."</td>
<td>".$row['mark']."</td>
</tr>";
}
}
else
{
echo "No Record Found";
}
}
else
{
echo "No Record Found";
}
}
?>
如果您的问题得到解决,请填写正确的答案
答案 3 :(得分:0)
<form action="" method="post">
<select name="class">
<option value="">-SELECT CLASS-</option>
<option value="Four">Four</option>
<option value="Five">Five</option>
</select>
<select name="sex">
<option value="">-SELECT GENDER-</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
<input type="submit" name="search" value="search" />
</form>
<?php
mysql_connect('localhost', 'root', '');
mysql_select_db ("test");
extract($_POST);
if(isset($search))
{
if($class!='' || $sex!='')
{
$wh='';
if($class!='') $wh.=" AND class='".$class."'";
if($sex!='') $wh.=" AND sex='".$sex."'";
$qry=mysql_query("SELECT * FROM student where 1 ".$wh."");
$total_record=mysql_num_rows($qry);
if($total_record>0)
{
echo "<table border='1'>
<tr>
<td>Name</td>
<td>Class</td>
<td>Sex</td>
<td>Mark</td>
</tr>
";
while($row=mysql_fetch_array($qry))
{
echo "<tr>
<td>".$row['name']."</td>
<td>".$row['class']."</td>
<td>".$row['sex']."</td>
<td>".$row['mark']."</td>
</tr>";
}
}
else
{
echo "No Record Found";
}
}
else
{
echo "No Record Found";
}
}
?>
&#13;
<form action="" method="post">
<select name="class">
<option value="">-SELECT CLASS-</option>
<option value="Four">Four</option>
<option value="Five">Five</option>
</select>
<select name="sex">
<option value="">-SELECT GENDER-</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
<input type="submit" name="search" value="search" />
</form>
<?php
mysql_connect('localhost', 'root', '');
mysql_select_db ("test");
extract($_POST);
if(isset($search))
{
if($class!='' || $sex!='')
{
$wh='';
if($class!='') $wh.=" AND class='".$class."'";
if($sex!='') $wh.=" AND sex='".$sex."'";
$qry=mysql_query("SELECT * FROM student where 1 ".$wh."");
$total_record=mysql_num_rows($qry);
if($total_record>0)
{
echo "<table border='1'>
<tr>
<td>Name</td>
<td>Class</td>
<td>Sex</td>
<td>Mark</td>
</tr>
";
while($row=mysql_fetch_array($qry))
{
echo "<tr>
<td>".$row['name']."</td>
<td>".$row['class']."</td>
<td>".$row['sex']."</td>
<td>".$row['mark']."</td>
</tr>";
}
}
else
{
echo "No Record Found";
}
}
else
{
echo "No Record Found";
}
}
?>
&#13;
哇 - 什么服务!感谢PROMPT回应我的问题。你的代码工作正常。我真的很感激。感谢