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

时间:2014-04-10 20:50:13

标签: mysql checkbox delete-row

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

  

警告:在第31行的/home/a9083956/public_html/zundappgroesbeek/beheer/testretrievesql.html中为foreach()提供的参数无效


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

  

解析错误:语法错误,意外' =',期待')'在/home/a9083956/public_html/zundappgroesbeek/beheer/testretrievesql.html第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>

1 个答案:

答案 0 :(得分:0)

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

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

请注意将=更改为=>

有关=&gt;的更多信息运营商,查看相关帖子here