Forum Moderators: coopster

Message Too Old, No Replies

I Cannot delete items from my db

         

cougar

7:51 am on Feb 26, 2005 (gmt 0)

10+ Year Member



Hello everybody,
I've written two scripts, delete1.php and delete-from-databse1.php. The purpose is to select an item in delete1.php sent it to delete-from-database1.php and then let delete-from-database1.php delete the previously selected item. But I only see a white page after posting the selected item in delete1.php.
Here are my scripts, I hope you guys know the answer:

===================delete1.php=====================
<html>
<head>
<title>ZDH GEETBETS</title>
<LINK rel="stylesheet" href="center.css" type="text/css">
</head>
<body class="body">
<?php
include("dbconnect.php");
$result = mysql_query("SELECT `titel`, `id` FROM id_center ORDER BY `id` ASC");
echo "<FORM method='post' action='delete-from-database1.php'><TABLE>";
while($row = mysql_fetch_assoc($result))
{
echo "</TR><TD><INPUT type='checkbox' name='".$row['id']."'></TD>";
echo "<TD>".$row['titel']."</TD></TR>";
}
echo "<TR><TD><INPUT type='submit' value='Delete'></TD></TR>";
echo "</TABLE></FORM>";
?>
</body>
</html>

===========delete-from-database1.php===============
<?php
error_reporting(E_ALL);
include("dbconnect.php");
foreach($_POST['$row['id']'] as $id_center)
{
$query = "DELETE FROM `id_center` WHERE id = $id_center";
echo $query;
$result = mysql_query($query) or die ('Fout: '.mysql_error());

if (mysql_affected_rows()!=0)
{
echo "<p>DELETE voltooid. <a href='delete1.php'>Klik hier om terug te gaan</a></p>";
}
else
{
echo "<p>DELETE afgebroken!</p>";
}
}
?>

dreamcatcher

8:47 am on Feb 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi cougar,

Welcome to WebmasterWorld. :)

Ok, you are using a foreach loop but not actually assigning your data to an array. You also have the wrong syntax for your check box:

<INPUT type='checkbox' name='".$row['id']."'>

Should be:

<INPUT type='checkbox' name="check[]" value='".$row['id']."'>

Note the [] after check. This creates an array of checkboxes.

Then your foreach loop would be:

foreach($_POST['$row['check']'] as $id_center)
{
$query = "DELETE FROM `id_center` WHERE id = $id_center";
//rest of code
}

If you are enabling users to delete things, you must use empty() before your foreach loop because if you try and execute the script with no box checked you will get an error.


if (!empty($_POST['check']))
{
foreach($_POST['$row['check']'] as $id_center)
{
//rest of code
}
echo "Entries deleted!";
}
else
{
echo "Please check something!";
}

Hope that helps.

dc

cougar

10:01 am on Feb 26, 2005 (gmt 0)

10+ Year Member



I've changed delete1.php and delete-from-database1.php. But I still get a white page after clicking delete in delete1.php

This is how the code looks like now

<?php
include("dbconnect.php");
foreach($_POST['$row['check']'] as $id_center)
{
$query = "DELETE FROM `id_center` WHERE id = $id_center";
echo $query;
$result = mysql_query($query) or die ('Fout: '.mysql_error());

// rest of code
}
?>

dreamcatcher

10:24 am on Feb 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Check your database connection file, the error might be there. Easy way is to access the connection file directly in your browser. You may see a parse error.

Also, make sure that your query uses apostrophes:

$query = "DELETE FROM `id_center` WHERE id = '$id_center'";

dc

cougar

2:36 pm on Feb 26, 2005 (gmt 0)

10+ Year Member



I've changed it but I still get a white page :s. And I can connect to my database because in delete1.php I select the items from my database to delete.

dreamcatcher

7:37 pm on Feb 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Strange. Well as there is no HTML on the destination page other than the paragraph tags from your foreach loop, it must be that the foreach loop is not executing.

Try changing:


<?php
error_reporting(E_ALL);
include("dbconnect.php");
foreach($_POST['$row['id']'] as $id_center)
{
$query = "DELETE FROM `id_center` WHERE id = $id_center";
echo $query;
$result = mysql_query($query) or die ('Fout: '.mysql_error());

if (mysql_affected_rows()!=0)
{
echo "<p>DELETE voltooid. <a href='delete1.php'>Klik hier om terug te gaan</a></p>";
}
else
{
echo "<p>DELETE afgebroken!</p>";
}
}
?>

to


<?php
error_reporting(E_ALL);
include("dbconnect.php");

$check = $_POST['check'];

foreach($check as $id_center)
{
$query = "DELETE FROM `id_center` WHERE id = '$id_center'";
$result = mysql_query($query) or die ('Fout: '.mysql_error());

if (mysql_affected_rows()!=0)
{
echo "<p>DELETE voltooid. <a href='delete1.php'>Klik hier om terug te gaan</a></p>";
}
else
{
echo "<p>DELETE afgebroken!</p>";
}
}
?>

dc

cougar

9:35 am on Feb 27, 2005 (gmt 0)

10+ Year Member



It works :d :d :d thanks m8 :)