mysql - 想让我的用户能够删除 mysql 行

标签 mysql checkbox delete-row

我将 = 更改为 =>。不幸的是我收到另一条错误消息:

Warning: Invalid argument supplied for foreach() in /home/a9083956/public_html/zundappgroesbeek/beheer/testretrievesql.html on line 31

<小时/>

我收到一条 php 错误消息。我做错了什么?

Parse error: syntax error, unexpected '=', expecting ')' in /home/a9083956/public_html/zundappgroesbeek/beheer/testretrievesql.html on line 31

我的 html 看起来像这样:

<html>
    <head>
    <title>Retrieve data from database </title>
    </head>
    <body>

    <?php
    // Connect to database server
    mysql_connect("mysql7.000webhost.com", "a9083956_test", "sesam") or die (mysql_error ());

    // Select database
    mysql_select_db("a9083956_test") or die(mysql_error());

    // SQL query
    $strSQL = "SELECT * FROM forum_question";

    // Execute the query (the recordset $rs contains the result)
    $rs = mysql_query($strSQL);

    // Loop the recordset $rs
    // Each row will be made into an array ($row) using mysql_fetch_array
    while($row = mysql_fetch_array($rs)) {

       // Write the value of the column FirstName (which is now in the array $row)
          echo '<input name="delete['.$row['id'].']" type="checkbox">';
      echo $row['topic']. " " .$row['name']. " " .$row['datetime'] . "<br />";
      }

$delete = $_POST['delete'];

foreach($delete as $id = $value)
{
    $id = mysql_real_escape_string($id);
    mysql_query("DELETE FROM table_name WHERE id = $id");
}

// Close the database connection
mysql_close();
?>
</body>
</html>

最佳答案

您的问题在于 foreach 循环,其中有 $id = $value。我怀疑你的意思是:

foreach($delete as $id => $value)
{
    $id = mysql_real_escape_string($id);
    mysql_query("DELETE FROM table_name WHERE id = $id");
}

请注意将 = 更改为 =>

有关 => 运算符的更多信息,请查看相关帖子 here .

关于mysql - 想让我的用户能够删除 mysql 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22998760/

相关文章:

javascript - 如何使用 Javascript 根据复选框的状态更改隐藏输入的值?

javascript - 如何访问以 '[]' 结尾命名的复选框组

checkbox - Vaadin - 如何将复选框组件添加到树中?

php - 通过连接 MySQL 和 PHP 选择过滤后的总和

php - MySQL 按文本排序

java - 如何按位置从数据库中删除行

iphone - 无法从SQLite数据库删除行,但未发出错误

ios - UITableView-删除部分中的最后一个单元格

mysql - mysql中涉及交叉连接时,COUNT如何表现?

php - 无法去除重复值