如何使用复选框删除查询

时间:2014-01-28 08:56:59

标签: php html mysql checkbox

如果我选中一个复选框并按删除,我正在努力编写将删除查询的代码。

更新:代码更新,你们提议,只有现在不起作用的东西 当复选框被选中并提交时,它将不会删除查询

这是我的时间:

$query="SELECT id,datum,titel,artikel FROM nieuws ORDER BY id DESC";
$result=mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo ("<div id=\"artikeltitel\" align=\"center\">
    <div id=\"containerdatum\">".$row['datum']."</div>
    <div id=\"containertitel\">".$row['titel']."</div>
    <div id=\"container3\" style=\"font-size:12px;\">".$row['id']."
    <input type=\"checkbox\" name=\"checkbox[]\" id=\"checkbox\"  value=\"".$row['id']."\"       />
    </div>
    </div>
    <div class=\"container\" align=\"center\">
    <h2 class=\"acc_trigger\"><a href=\"#\"> &#187; </a></h2>
    <div class=\"acc_container\">
    <div class=\"block\">".$row['artikel']."</div>
    <div class=\"fb-comments\" data-href=\"http://www.zpb-polonez.be/user.php\" data-num-    posts=\"10\" data-width=\"678\" style=\"margin-top:2px;\"></div>
    </div>
    </div>
    ");

} 这是我读取我的sql的部分,不知道它是否一切正常,大部分代码 我复制了我在学校学到的其他作品

require_once("inc/connection.php");
mysql_select_db("nieuws");
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$id = $_POST['id'];
$datum = $_POST["datum"];
$titel = $_POST["titel"];
$artikel = $_POST["artikel"];
$checkbox = $_POST["checkbox"];
$titel = mysql_real_escape_string(nl2br(htmlentities($_POST["titel"])));
$artikel = mysql_real_escape_string($_POST["artikel"]);
$id = mysql_real_escape_string($_POST['id']);
date_default_timezone_set('GMT');
$datum = date('Y-m-d', strtotime(str_replace('-', '/', $datum)));
    if(isset($_POST['add'])){
        if(!empty($_POST['titel']) && !empty($_POST['artikel']) && !empty($_POST['datum'])){
        $query="INSERT INTO nieuws (id,datum,titel,artikel) VALUES ('$id','$datum','$titel','$artikel')";
        $datum = date('Y-m-d', strtotime(str_replace('-', '/', $datum)));
        str_replace('<br />', "\n", $textarea);
        $result=mysql_query($query);
        header("location: user.php");
        $juist1 = true;
        }else{
        $fout1 = true;
        }
    }if(isset($_POST['delete'])){
        foreach($_POST['checkbox'] as $del_id){
        $del_id=$checkbox; 
        $sql="DELETE FROM nieuws WHERE id='$del_id'";
        $result = mysql_query($sql);
        header("location: user.php");
        }
    }

}

已提交

<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
<input type="submit" name="delete" value="verwijderen" />
</form>

4 个答案:

答案 0 :(得分:0)

试试这个,

删除脚本:

if(isset($_POST['delete'])){
 foreach($_POST['checkbox'] as $del_id){
    $sql="DELETE FROM nieuws WHERE id='$del_id'";
    $result = mysql_query($sql);
 }
 header("location: user.php");
}

剧本:

<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
<?php
$query="SELECT id,datum,titel,artikel FROM nieuws ORDER BY id DESC";
$result=mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_row($result)){
    echo ("<div id=\"artikeltitel\" align=\"center\">
        <div id=\"containerdatum\">".$row['datum']."</div>
        <div id=\"containertitel\">".$row['titel']."</div>
        <div id=\"container3\" style=\"font-size:12px;\">".$row['id']."
        <input type=\"checkbox\" name=\"checkbox[]\" id=\"checkbox\"  value=\"".$row['id']."\"       />
        </div>
        </div>
        <div class=\"container\" align=\"center\">
        <h2 class=\"acc_trigger\"><a href=\"#\"> &#187; </a></h2>
        <div class=\"acc_container\">
        <div class=\"block\">".$row['artikel']."</div>
        <div class=\"fb-comments\" data-href=\"http://www.zpb-polonez.be/user.php\" data-num-    posts=\"10\" data-width=\"678\" style=\"margin-top:2px;\"></div>
        </div>
        </div>
        ");
 }?>
 <input type="submit" name="delete" value="verwijderen" />
 </form>

答案 1 :(得分:0)

只是看看你提供的内容,我无法看到你在$ row的位置,以便用$ row ['id']填充复选框。如果你只尝试$ id而不是$ row ['id']会怎么样?

<input type=\"checkbox\" name=\"checkbox[]\" id=\"checkbox\"  value=\"$id\" />

如果您检查HTML,是否正确填充了复选框的值?

答案 2 :(得分:0)

try this
if(isset($_POST['checkbox'])){
    foreach($_POST['checkbox'] as $del_id){
        $sql="DELETE FROM nieuws WHERE id='$del_id'";
        $result = mysql_query($sql);
     }
    header("location: user.php");
}

答案 3 :(得分:0)

<form > //open form
<?php
while loop(conditon)
{?>
<input type="checkbox" name="checkbox_val[]" id="checkbox"  value="<?php echo 
$row['id'];?>"/>
<?php
} ?>
<input type="submit" value="Submit" name="btn_post">
</form >//closing
<?php
if(isset($_POST['btn_post']){
$check_val= $_POST['checkbox_val'];
foreach($check_val as $chval)
{
   $sql="DELETE FROM nieuws WHERE id='$chval[0]'";
    $result = mysql_query($sql);
}
}
?>