2012-09-19 93 views
0

我已经使用下面的代码来为选中的复选框选择多个值,以便将其从数据库表中删除,但是当我是print_r时,它仅显示其不显示id值的键。如何获取php中的多个复选框的值

我使用此代码在数组中获取价值: -

<?php 
echo "Hiiiiiiiiii"; 
include("conn.php"); 
$sql="select * from test "; 

$res=mysql_query($sql) or die(mysql_error()); 
?> 

<form name="form1" method="POST" action=""> 
    <table width="578" border="1" align="center" id="menu"> 
    <tr> 
    <th></th> 
    <th>id</th> 
    <th>Name</th> 
    <th>email</th> 
    <th>phno</th> 
</tr> 

<?php 
while($row=mysql_fetch_array($res)) 
{ 
?> 

<tr> 

    <td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td> 
    <td><?php echo $row['id'];?></td> 

    <td><?php echo $row['name'];?></td> 
    <td><?php echo $row['emailid'];?></td> 

    <td><?php echo $row['phno'];?></td> 
    <?php 
    echo"<td><a href='update.php?id=".$row['id']."'>Update</a></td>"; 
    ?> 
<?php 
    } 
?> 
<tr><td><input type="submit" name="delete" value="Delete" id="delete"></td></tr></tr></table> 

<?php 
// Check if delete button active, start this 
$count = mysql_num_rows($res); 
echo "$count"; 

if(isset($_POST['delete'])) 
{ 
    if(count($_POST['checkbox']) !=0) 
    { 
     $array = array("checkbox" => $_POST['checkbox']); 
     print_r($array); 
     $ids = implode(',', $array); 
     echo "$ids"; 
     $result = mysql_query("DELETE FROM test WHERE id IN ($ids)") or die(mysql_error()); 
    } 
} 
// if successful redirect to delete_multiple.php 
if($result) 
    { 
    echo "<meta http-equiv=\"refresh\" content=\"0;URL=teach_delinquent.php\">"; 
    echo "SUCCESS"; 
    } 

?> 
+0

试试'var_dump($ _ POST);'。 – 2012-09-19 06:44:25

+0

你不能有id =“复选框[]”,这需要设置为一个唯一的值。 – jtheman

+0

$ _POST ['checkbox']已经是来自postdata的一个数组,所以你不需要使它变得更安全。 – jtheman

回答

0

$_POST['checkbox']阵列将仅包含选中的复选框:

$ids = array(); 
foreach($_POST['checkbox'] as $val){ 
    $ids[] = (int) $val; 
} 
$ids = implode(',', $ids); 

我已经做了foreach使所有的ID INT为安全目的。

0

你的问题的快速修复方案如下:

替换以下行

$array = array("checkbox" => $_POST['checkbox']); 

$array = $_POST['checkbox']; 

$array = array_map('intval',$_POST['checkbox']); // if all values are integer ---for security 

因为$ _POST [ '复选框']已经是一个数组

希望这能解决您的问题