Click to See Complete Forum and Search --> : [RESOLVED] MySQL Delete Statement


monarch_684
07-05-2007, 09:42 AM
This is what I have:

<?php
$id = $_POST["id"];

$query = "DELETE FROM information WHERE id = '{$id}' ";

$connection = mysql_connect("localhost", "username", "password");
@mysql_select_db("info_scroller", $connection)
or die( "Unable to select database" . mysql_error());

$result = mysql_query($query)
or die ("Couldn't query data" . mysql_error());

mysql_close();
?>


I am linking this from a delete link on a display screen:

<?php

$connection = mysql_connect("localhost", "username", "password");
@mysql_select_db("info_scroller", $connection)
or die( "Unable to select database" . mysql_error());

$query = "SELECT * FROM information";
$result = mysql_query($query)
or die ("Couldn't query data" . mysql_error());

echo "<table border='1'>";
echo "<tr>";
echo "<th>ID</th><th>Info</th><th>Submitted By</th>";
echo "</tr>";

while ($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td name=\"id\">", $row['id'], "</td><td>", $row['info'],"</td><td>", $row['submit_by'], "</td><td><a href='delete.php'>delete</a> | edit</td>";
echo "</tr>";
}

echo "</table>";

mysql_close();
?>


For some reason this is not deleting the data. Any ideas?

holyhttp
07-05-2007, 10:25 AM
If the target record is not delete that can only mean the SQL statement does not specify the right $id.
Try displaying the "DELETE" query to see if the $id value is well specified.

If this <td><a href='delete.php'>delete</a> | edit</td>"; is the HTML code for the DELETE link then you have 2 issues:
1- The id is not specified in the url and must be href='delete.php?id=".$row['id']."'
2- The in your PHP code you should have $id=$_GET["id"] instead

You would use $id=$_POST["id"] if the date comes from a form with a POST as the method for sending the form data.

monarch_684
07-05-2007, 11:16 AM
Thank you much. It worked. I am still very new at mysql.