douzhong1907 2010-12-01 17:07
浏览 94
已采纳

如何在POST中发送多个复选框检查以从数据库中删除?

I've been trying think of a way to do this. I want it to where users can check off items, hit submit and it goes to the code on the next page and deletes all of the checked items from the database. Problem one is that in the post its only sending over the last checked item. Here is how I have it set up right now.

echo "<form name='fm1' METHOD ='POST' ACTION ='displaydelete.php' > ";

//Draws up the table headers echo ""; echo ""; echo "Fund Number "; echo "Hours "; echo "Percentage"; echo "Delete"; echo ""; //While there are query results data is pushed into table cells while ($row = mysql_fetch_array($queryResult2)) { $hours = $row['hours']; $percentage = $hours / 160 * 100;

   echo "<tr>";
    echo "<td>";
    echo $row['funnumber'];
    echo "</td>";
    echo "<td>";
    echo $hours;
    echo "</td>";
    echo "<td>";
    echo $percentage ."%";
    echo "</td>";
    echo "<td>";
   echo "<input type='checkbox' name='id' value='$row[id]'/>";
  echo "</td>";
    echo "</tr>";




}
    //End of tabel 
echo "</table>";

echo" "; echo "";

What I would like to do is push all of the items into a variable and maybe delete them that way. I'm not really sure how you would handle multiple deletes. I'm doing my delete like this for something else if this helps any.

 $query = "DELETE FROM users

WHERE ninenumber = '$ninenumber'"; $result = mysql_query($query) or die("Query Failed: " .mysql_error());

        mysql_close($conn);
  • 写回答

6条回答 默认 最新

  • dongliyan7318 2010-12-01 17:11
    关注

    Instead of this:

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

    You need this:

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

    Note the difference. I added [] after the input name. This tells the client and server that there are multiple inputs with that name. $_POST['id'] will be an array you can loop through on the next page.

    foreach ($_POST['id'] as $checkbox) {
        // DELETE FROM users WHERE ninenumber = $checkbox
    }
    

    isset, is_array, and mysql_real_escape_string omitted for brevity.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路