Jump to content

[SOLVED] Delete record button not working


Ty44ler

Recommended Posts

I have a site where users submit information that is displayed on another page. I have a delete button below the information that should delete it from my database... It says it's successful but when I check the database the records are still there...

 

Form on the display page:

$sql = "select * from users order by id DESC";
$result = mysql_query ($sql);

if(!isset($cmd)) 
{

while ($row = mysql_fetch_array($result))
{
$id=$row["id"];

echo "
<h2>$id</h2>

<form action='delete.php' method='POST'>
<input type='submit' value='delete' name='id' />
</form>

 

 

Code for delete.php:

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// get value of id that sent from address bar
$id=$HTTP_POST_VARS['$id'];

// Delete data in mysql from row that has this id
$sql="DELETE FROM $tbl_name WHERE id='$id'";
$result=mysql_query($sql);

// if successfully deleted
if($result){
echo "Deleted Successfully";
echo "$id";
echo "<a href='display.php'>Back to main page</a>";
}

else {
echo "ERROR";
}

// close connection
mysql_close();

Link to comment
https://forums.phpfreaks.com/topic/171307-solved-delete-record-button-not-working/
Share on other sites

$HTTP_POST_VARS was depreciated over 7 years ago, turned off by default in php5, and completely removed in php6.

 

Use $_POST instead.

 

Your code displays the "Deleted Successfully" message if the query is executed at all. You would actually need to test the value that mysql_affected_rows() returns to know if the query deleted anything.

print $sql doesn't say anything. I don't think the variable is getting passed correctly. I changed the $id to an actual id number and it deletes it correctly, so I think it has to do with the variable. I also changed $HTTP_POST_VARS to $_POST (someone told me to try and change it that way to see if that was the problem).

I figured it out....

 

Here's my new code if anybody was wondering...

 

<form action='delete.php' method='POST'>
<input type='hidden' value='$id' name='id' />
<input type='submit' value='delete' name='delete' />
</form>

 

and...

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// get value of id that sent from address bar
$id=$_REQUEST["id"];

// Delete data in mysql from row that has this id
$sql="DELETE FROM $tbl_name WHERE id='$id'";
$result=mysql_query($sql);

// if successfully deleted
if($result){
echo "$id Deleted Successfully ";
echo "<a href='display.php'>Back to main page</a>";
}

else {
echo "ERROR";
}

// close connection
mysql_close();

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.