如何使用带有表单删除按钮的php从mysql数据库中删除行 [英] How to delete rows from a mysql database using php with a form delete button

查看:62
本文介绍了如何使用带有表单删除按钮的php从mysql数据库中删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的可以使用一些帮助。我对所有人都有些陌生。我正在尝试制作一种工具来从数据库表中删除行。这就是我到目前为止所想的,它可以显示表中的行,并带有链接到该行的复选框和一个删除按钮以删除选定的行。但是,当我单击Delete时,会收到以下错误消息。

I could really use some help. I am a bit new to all. And I am trying to make an tool to delete rows from a table of my database. This is what I came up with so far, it can show the rows from the table with a checkbox linked to that row and a delete button to delete the selected rows. But when I hit delete I get the following error message.


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

Warning: mysql_real_escape_string() expects parameter 1 to be string, array given in /home/a9083956/public_html/zundappgroesbeek/beheer/testretrievesql.html on line 31

在此代码下方:

<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>


推荐答案

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

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

And I will not even mention switching to mysqli.

这篇关于如何使用带有表单删除按钮的php从mysql数据库中删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆