Jump to content

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();

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.