Jump to content

[SOLVED] Problem with my first MySQL Delete Script


comp_eng_

Recommended Posts

Hello,

 

I've just started to learn PHP/MySQL from some online tutorials

I'm currently working on my first PHP script for deleting a row from my db

 

I'm facing 2 problems with the code:

 

1- It's not deleting the selected row

 

2- It's only displaying the rows that have Even indices

 

Hope u can help me  ;)

 

this is my code:

 

<?php //connect to mysql$con = mysql_connect("localhost","###","###" , $con ) or die('Unable to connect to DB.<br>'.mysql_connect_error());
// connect or show error message
mysql_select_db("sm_FSpeaches") or die('Unable to select DB.'); 
//select or show error message
//If cmd has not been initialized
// Specifying that cmd is coming from a GET request helps keeping the script running, 
// in case the option register_globals was turned off (for security reasons)

if(!isset($_GET["cmd"])) 
{
//display all the rows 
$result = mysql_query("SELECT * FROM Speaches order by No") or die('Query failed.'); 
//run the while loop that grabs all the rows 

while($r=mysql_fetch_array($result)) {	 
echo '<a href="delete.php?cmd=delete&No='.$r["No"].'>Delete '.$r["No"].' - '.$r["Title"].'</a><br>';	 
//make the title a link 
}
}
else if($_GET["cmd"]=="delete"){		
$num=$_GET["No"]; 
//MUST BE FILTERED TO AVOID CODE-INJECTION ATTACKS!       	
mysql_query("DELETE FROM Speaches WHERE No=$num");	
if (!mysql_query("DELETE FROM Speaches WHERE No=$num")) echo 'Could not delete!';	
else echo 'Row delete.';        

}
else {
echo 'Uh oh! Someone made a bobo!<br>Are you playing with the url? DONT!';
}
@mysql_free_result($result); 
//clean & dont show errors if any
mysql_close($con);
?>

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.