0

我有一个 HTML 表单,其中包含一些记录。应通过选择一个或所有记录对应的复选框来删除记录。

当我选中一条记录的复选框并单击删除时,查询成功运行,但记录没有删除。

这是PHP代码:

if(isset($_POST['delete']))
{ 
    $allCheckBoxId = $_POST['chkDel'];

    array_map ('mysql_real_escape_string', $allCheckBoxId);
    $ids = implode(",", $allCheckBoxId);

    $object=new connection();
    $object=$object->dbConnect();


    $st=$object->prepare(
        "delete * from banners where bannerid IN (?)");

    $st->bindParam(1, $ids);
    $delete=$st;
    $delete->execute();
    if($delete)
    {
        echo "record deleted successfully." . 
             "This will go to banners within 5 sec";

    }
    else
    {
        echo "not deleted";

    }
}

和 HTML 表单:

<form name="myform" action="listbanners.php" method="post" OnSubmit="return onDelete();" id="frm1">
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
        <th width="13"><input type="checkbox" class="checkbox" title="select all" onclick="checkAll(document.myform.chkDel);" name="CheckAll" /></th>
        <th width="13">ID</th>
        <th>Name</th>
        <th><input type="submit" name="delete" value"Delete"></th>
    </tr>
    <?php
        $object=new connection();
        $object=$object->dbConnect();
        $st=$object->prepare("select * from banners order by bannerid desc");
        $st->execute();
        while($row=$st->fetch(PDO::FETCH_ASSOC))
        {
    ?>

    <tr>
        <td><input name="chkDel[]" type="checkbox" title="select" class="checkbox" value="<?php echo $row['bannerid'];?>"/></td>
        <td><?php echo $row['bannerid'];?></td>
        <td><h3 onClick="location='editbanner.php?bannerid=<?php echo $row['bannerid'];?>'"><?php echo $row['file'];?></h3></td> 
        <td><input type="submit" name="delete" value="delete"/></td>
    </tr>
</form>
4

1 回答 1

2
$st=$object->prepare("delete * from banners where bannerid IN (?)");
$st->bindParam(1, $ids);

您正在尝试将单个参数绑定到值数组。您需要将每个值作为单独的参数传入;例如:

$st=$object->prepare("delete * from banners where bannerid IN (?,?,?)");

您需要为要包含在 IN 子句中的每个值包含一个占位符。您可以根据 中的项目数动态生成 SQL $ids

我错过了'DELETE *',这是无效的语法;但我把它留在原地,因为我注意到的一点是有效的。这是一个不完整的答案,不是错误的。

于 2012-11-09T17:19:58.437 回答