Jump to content

Recommended Posts

Hey there,

I have a very basic php code to delete single row in a table, the problem is that it doesn't work  :wtf: here it is:

<html>
<body>

<? 

//connect to mysql


mysql_connect("localhost","username","password");



//select which database you want to edit	
mysql_select_db("db_namer"); 


//If cmd is not hit	
if(!isset($cmd)) 
{
   
	//display all the files   
	$result = mysql_query("select * from tbl_name"); 
   
   

	//run the while loop that grabs all the names in table
	while($r=mysql_fetch_array($result))    
	{       
		//grab the title and the ID       
		$title=$r["name"];			//take out the title      
		$id=$r["id"];				//take out the id    
      

		echo "<a href='delete.php?cmd=delete&id=$id'>$title - Delete</a>";      
		echo "<br>";
    
	}
}
?>

<?
if($cmd=="delete")
{    
$sql = "DELETE FROM tbl_name WHERE id = $id";
    
$result = mysql_query($sql);    

echo "Row deleted!";	
}

?>


</body>

</html>

it returns all records that are in table, but when I hit delete - nothing. It is probably something simple, but I just cant figure it out by my self.

Thanx

Link to comment
https://forums.phpfreaks.com/topic/183116-phpmysql-delete-row-script/
Share on other sites

Your code is dependent on register_globals being ON to magically populate $cmd and $id from the $_GET variables on the end of the URL. Where ever you found or learned that code, it is 7 years out of date because register_globals were turned off by default in php4.2 in April of the year 2002 and no new code, books, or tutorials should have been written after that point in time that relied on register_globals being on.

 

You should also be learning php, developing php code, and debugging php code on a system with error_reporting set to E_ALL and display_errors set to ON in your master php.ini so that php will help you. You will save a ton of time. There would have been undefined error messages concerning $cmd and $id that would have alerted you to the fact that they are not being set.

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.