选择多个项目以执行任务

时间:2016-02-05 08:46:48

标签: javascript php mysql

我想选择多个项目来删除数据库中的记录。但我无法做到。我的delete.php没问题。如何使用它删除多个选定的项目。我刚刚创建了一个复选框。如何使该复选框工作。

echo"<TR><TD>Select</td><TD>S.N.</td><td><B>Full Name</B></td><td><B>Options</B></td></TR>";
while ($myrow = $result->fetch_assoc())
{
    echo "<tr><td><input type='checkbox'name='mycheck'onclick='toggleform('document.myform','mycheck','ptext')'></td>";
    echo "<TD>".$myrow['id']."</TD>";
    echo "<TD>".$myrow['name']." </TD>";
    echo "<TD><a href=\"view1.php?id=".$myrow['id']."\">View</a> ";
    echo "<a href=\"delete.php?id=".$myrow['id']."\">Delete</a> ";
    echo "<a href=\"edit1.php?id=".$myrow['id']."\">Edit</a>";
}
<input type="button" name= "ptext" value="Delete selected">

2 个答案:

答案 0 :(得分:1)

您必须将checkbox name创建为array类型,即mycheck[]。然后,在 delete_all.php 页面中,找到总复选框并相应地删除它。

<form method='POST' action="delete_all.php">

    <?php
    echo"<TR><TD>Select</td><TD>S.N.</td><td><B>Full Name</B></td><td><B>Options</B></td></TR>";
    while ($myrow = $result->fetch_assoc())
    {
          echo "<tr><td><input type='checkbox' name='mycheck[]' value=".$myrow['id']."></td>";
          echo "<TD>".$myrow['id']."</TD>";
          echo "<TD>".$myrow['name']." </TD>";
          echo "<TD><a href=\"view1.php?id=".$myrow['id']."\">View</a> ";
          echo "<a href=\"delete.php?id=".$myrow['id']."\">Delete</a> ";
          echo "<a href=\"edit1.php?id=".$myrow['id']."\">Edit</a>";
    }
    ?>
    <input type="submit" name= "ptext" value="Delete selected">

</form>

<强> delete_all.php

<?
extract($_POST);

$totalCheckboxChecked = sizeof($mycheck);

for($i=0;$i<$totalCheckboxChecked;$i++)
{
    $idToDelete = $mycheck[$i];

    echo "DELETE FROM table_Name WHERE id_ColumnName = '$idToDelete'";

    // Execute Your Querys
}
?>

答案 1 :(得分:0)

您是否在delete.php页面上获得了所有ID?如果是,那么简单使用

DELETE FROM tableName
WHERE id IN ($id1 , $id2 , $id3 , etc);

如果没有,我会这样做,我会使用表单,然后使用方法发布在delete.php页面中发布,然后简单地从超级全局中取值

$_POST[]

并使用上面的查询,希望这有帮助。