如何使用带有表单删除按钮的php从mysql数据库中删除行

时间:2014-05-02 09:44:45

标签: php mysql delete-row

我真的可以使用一些帮助。我对所有人都有点新意。我正在尝试创建一个工具来删除数据库表中的行。这是我到目前为止所提出的,它可以显示表格中的行,其中包含链接到该行的复选框和删除所选行的删除按钮。但是当我点击删除时,我收到以下错误消息。

  

警告:mysql_real_escape_string()期望参数1为字符串,数组在第31行的/home/a9083956/public_html/zundappgroesbeek/beheer/testretrievesql.html中给出

在这下面的代码:

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

<form action="" method="post">

    <?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[]" type="checkbox">';
    echo $row['topic']. " " .$row['name']. " " .$row['datetime'] . "<br />";

}
$delete = mysql_real_escape_string($_POST['delete']); 
for($i=0; $i < count($delete); $i++){
    mysql_query("DELETE FROM table_name WHERE id = $delete[$i] ");
}


// Close the database connection
mysql_close();
?>

<input type="submit" value="Delete selected items">

</form>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

mysql_query("DELETE FROM table_name WHERE id = " . (1 * $delete[$i]));

我甚至不会提到切换到mysqli。