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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>";
}}}

Link to comment
Share on other sites

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.