Jump to content

Probably Simple DELETE WHERE quation


melting_dog

Recommended Posts

I'm trying to implement a code that will simply delete a row via a pre-entering an ID. Pretty simple, but I seem to be missing something so some other opinions would be appreciated.

 

The postID is sent from a form. So:

 

$postID=$_POST['postID'];

$postID = stripslashes($postID);

$postID = mysql_real_escape_string($postID);

 

$sql="SELECT * FROM $tbl_name WHERE postID='$postID'";

$result=mysql_query($sql);

$count=mysql_num_rows($result);

 

if($count==1){

$sql="DELETE FROM $tbl_name WHERE postID='$postID' LIMIT 1";

$result = '<p>Successfully Removed</p><a href="index.php">Back to Home Page</a>';

}

 

else {

$result = '<h3 style = "text-align: center;">Sorry! No Posts with that ID were found.</h3><a href="removePost.php">Try Again?</a>';

}

ob_end_flush();

?>

 

Yeah so like i said probably something simple but any help would be greatly appreciated

 

Cheers

Link to comment
https://forums.phpfreaks.com/topic/206037-probably-simple-delete-where-quation/
Share on other sites

<?php
$postID=(int)$_POST['postID']; //assuming ID is numeric, that's even better than real_escape()

$sql="DELETE FROM $tbl_name WHERE postID='$postID' LIMIT 1";
mysql_query($sql) or die(mysql_error()); // some better error handling should be put in place instead of this

if(mysql_affected_rows() > 0) {  //this will tell you if any rows were deletod or not
  $result = '<p>Successfully Removed</p><a href="index.php">Back to Home Page</a>';
} else {
$result = '<h3 style = "text-align: center;">Sorry! No Posts with that ID were found.</h3><a href="removePost.php">Try Again?</a>';
}
echo $result;
ob_end_flush();
?>

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.