Jump to content

Command executes but doesnt really do it in MySQL?


jmrothermel

Recommended Posts

Heres the situation.  Im using the following code:

 

if($a == 'delete') {
$id = $_POST['id'];

$delete = mysql_query("DELETE FROM `adminrecord` WHERE `id`='$id'");
	if($delete) {
 	print "Record Deleted! <a href='chartview.php'>Return to Admin Chart</a>";
	}
	else {
	 	print mysql_error();
	}

	} 

 

And when I click the correct link I get the confirmation page. Record Deleted! etc etc etc but it doesnt really delete it from the database.  Any suggestions?  Thank you for all your help everyone - this site has gotten me through alot the past few days!

try...

if($a == 'delete') {
$id = $_POST['id'];

mysql_query("DELETE FROM `adminrecord` WHERE `id`='$id'") or die (mysql_error());
print "Record Deleted! <a href='chartview.php'>Return to Admin Chart</a>";
}

 

1second your code in insecure asweel vunerable to sql injection

From the php manual for mysql_query -

 

Return Values

...

For other type of SQL statements, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

 

Use ... mysql_affected_rows() to find out how many rows were affected by a DELETE, INSERT, REPLACE, or UPDATE statement.

 

Your query is not failing but is probably not affecting any rows. You should check the mysql_affected_rows() value in the code to make sure before echoing the "Record Deleted!" message.

 

You should build your query into a string variable and then echo it to see what it actually contains and then make sure that the value in $id actually exists in the database.

Heres a modified version of your script to check if that id exists...

 

if($a == 'delete') {
$id = $_POST['id'];

if (is_numeric($id) == false){ //PROVIDES SOME SECURITY
print "Invalid Id";
}else{

$check= mysql_query("SELECT * FROM adminrecord WHERE `id`='$id'");
$check_num= mysql_num_rows($check);

if ($check_num == "0"){
print "Record could not be found";
}else{

mysql_query("DELETE FROM `adminrecord` WHERE `id`='$id'") or die (mysql_error());
print "Record Deleted! <a href='chartview.php'>Return to Admin Chart</a>";
}}}

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.