1

次のコードを使用して、選択したチェックボックスに複数の値を選択し、データベーステーブルから削除しましたが、print_rの場合、idの値が表示されないためのキーのみが表示されます。

私はこのコードを使用して配列の値を取得しました:-

<?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";
    }

 ?>
4

3 に答える 3

0

$_POST['checkbox']配列にはチェックボックスのみが含まれます:

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

セキュリティ上foreachの理由から、すべての ID を作成しました。INT

于 2012-09-19T06:44:47.150 に答える
0

あなたの質問のクイックフィックスは次のとおりです

下の行を置き換えます

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

$array = $_POST['checkbox'];

また

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

$_POST['checkbox'] はすでに配列であるため

これで問題が解決することを願っています

于 2016-03-28T04:52:10.043 に答える