Jump to content

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();
?>

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.