2016-03-08 83 views
0

我有几个复选框,并单击多个复选框时&提交表单时它会在数据库搜索相应的结果,但它给我注意:如何使用爆炸检索多个复选框值?

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> 
+0

['foreach'是你的朋友(http://php.net/manual /en/control-structures.foreach.php) – Prix

回答

0

explode()功能分割字符串转换成阵列。

implode()函数将数组转换为字符串。

因此,它需要字符串作为第二个参数。您的复选框branch[]已经作为数组发布。

$_POST['branch']是一个数组。您不需要爆炸()复选框数组。我想你需要将数组组合成一个字符串。

因此,使用implode()

只需使用:

$arrayName2 = $_POST['branch']; 
$branch=implode(",",$arrayName2); 

而不是

$arrayName2 = $_POST['branch']; 
$branch=explode(",",$arrayName2); 
+0

谢谢!我摆脱了错误,但现在它向我显示新的错误是:警告:无效的参数提供的foreach()在C:\ xampp \ htdocs \ enoticeboard \ home.php在线78 –

+0

'$ userRow2 ['doc' ]'没有获得价值。你应该添加一个条件:'if(!empty($ userRow2 ['doc'])){...' – Pupil

+0

给我同样的错误没有改变 –

0

的警告是明确explode()要求参数2是字符串,而是要传递阵中还有。如果你想加入用逗号数组元素,那么你应该使用implode()如下:

$hostel=implode(",",$arrayName); 
$branch=implode(",",$arrayName2); 
$year=implode(",",$arrayName3); 
0

试试这个

if(isset($_POST['Submit'])) { 
    echo "<pre>"; 
    print_r($_POST); 
    $checked = implode(',', $_POST['checkbox']); 
    echo $checked; 
}