0

I changed the = to =>. Unfortunately I get another error message:

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


I am getting a php error message. What am i doing wrong?

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

My html looks like this:

<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>
Unihedron
  • 10,251
  • 13
  • 53
  • 66
M Lanser
  • 23
  • 3
  • possible duplicate of [How to delete rows from a mysql database using php with a form delete button](http://stackoverflow.com/questions/23425147/how-to-delete-rows-from-a-mysql-database-using-php-with-a-form-delete-button) – Unihedron Oct 25 '14 at 07:35

1 Answers1

0

Your issue lies in the foreach loop, where you have $id = $value. I suspect you mean:

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

Note the changing of = to =>.

For more about the => operator, view the related post here.

Community
  • 1
  • 1
Aaron St. Clair
  • 183
  • 1
  • 2
  • 10
  • Thanks alot i changed it but now i got another error message.Warning: Invalid argument supplied for foreach() in /home/a9083956/public_html/zundappgroesbeek/beheer/testretrievesql.html on line 31 – M Lanser Apr 11 '14 at 09:50