Question

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.

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

Beneath this the code:

<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>
Was it helpful?

Solution

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

And I will not even mention switching to mysqli.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top