我有几个复选框,并单击多个复选框时&提交表单时它会在数据库搜索相应的结果,但它给我注意:如何使用爆炸检索多个复选框值?
Warning: explode() expects parameter 2 to be string, array given in C:\xampp\htdocs\enoticeboard\home.php on line 68
这个错误是3倍。
我如何成功检索它? 我的代码是:
<?php
include_once 'dbconfig.php';
if(!$user->is_loggedin())
{
$user->redirect('index.php');
}
$user_id = $_SESSION['user_session'];
$stmt = $DB_con->prepare("SELECT * FROM users WHERE user_id=:user_id");
$stmt->execute(array(":user_id"=>$user_id));
$userRow=$stmt->fetch(PDO::FETCH_ASSOC);
?>
<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css" type="text/css" />
<link rel="stylesheet" href="style.css" type="text/css" />
<title>welcome -
<?php print($userRow[ 'user_email']); ?>
</title>
<style>
.left {
margin-left: 20px;
width: 100px;
}
</style>
</head>
<body>
<div class="header">
<div class="left">
<label><a>Welcome : <?php print($userRow['user_name']); ?></a> </label>
</div>
<div class="right">
<label><a href="logout.php?logout=true"><i class="glyphicon glyphicon-log-out"></i> logout</a>
</label>
</div>
</div>
<div class="content">
<h1>Notice Board</h1>
<div class="left">
<h4>Select Hostel,Branch,Year</h4>
<form action="home.php" method="POST">
<label>Hostel</label>
<br/>
<input type="checkbox" name="hostel[]" value="K.P-1">K.P-1</input>
<br/>
<input type="checkbox" name="hostel[]" value="K.P-2">K.P-2</input>
<br/>
<input type="checkbox" name="hostel[]" value="K.P-3">K.P-3</input>
<br/>
<label>Branch</label>
<br/>
<input type="checkbox" name="branch[]" value="CSE">CSE</input>
<br/>
<input type="checkbox" name="branch[]" value="I.T">I.T</input>
<br/>
<input type="checkbox" name="branch[]" value="CIVIL">CIVIL</input>
<br/>
<label>YEAR</label>
<br/>
<input type="checkbox" name="year[]" value="2011">2011</input>
<br/>
<input type="checkbox" name="year[]" value="2012">2012</input>
<br/>
<input type="checkbox" name="year[]" value="2013">2013</input>
<br/>
<input type="submit" value="See Notices" />
</form>
</div>
<?php
if(isset($_POST['hostel']) && isset($_POST['branch']) && isset($_POST['year']))
{
if(!empty($_POST['hostel']) && !empty($_POST['branch']) && !empty($_POST['year']))
{
$arrayName = $_POST['hostel'];
$arrayName2 = $_POST['branch'];
$arrayName3 = $_POST['year'];
$hostel=explode(",",$arrayName);
$branch=explode(",",$arrayName2);
$year=explode(",",$arrayName3);
$stmt2=$DB_con->prepare("SELECT * FROM doc WHERE hostel=:hostel AND branch=:branch AND year=:year");
$stmt2->execute(array(':hostel'=>$hostel,':branch'=>$branch,':year'=>$year));
$userRow2=$stmt2->fetch(PDO::FETCH_ASSOC);
?>
<div class="middle">
<?php
if($userRow2 > 0){
foreach($userRow2['doc'] as $value2){
print_r($value2['doc']);
echo 'hi';
}
}
else {
echo "No records.";
}
?>
</div>
<?php
}
else {
echo "No Notice Found.";
}
}
else {
echo "Please fill in all fields.";
}
?>
</body>
</html>
['foreach'是你的朋友(http://php.net/manual /en/control-structures.foreach.php) – Prix