简体   繁体   中英

SQL code in php not deleting record

First: Yes mysqli is awesome, i cannot use it for this.

now: I wrote a code that once called should delete a specific record, it executes, no errors, and output message even comes out but it does not delete the record.

why? How can I fix this?

I see no mistake so far:

 <?php if(isset($_POST['Delete'])) { $connection = mysql_connect("Deleted login info"); // Check connection if (!$connection) { //echo "Connection failed: " . mysql_connect_error(); } else { //select a database $dbName="Katz"; $db_selected = mysql_select_db($dbName, $connection); //confirm connection to database if (!$db_selected) { die ('Can\\'t use $dbName : ' . mysql_error()); } else if ($_POST[KittenID]=='') { $OutputMessage = 'Must add a Kitten-ID'; } else {//exeption else {//main else $sql = "DELETE FROM Kittenzz WHERE KittenID='$_POST[KittenID]"; $retval = mysql_query($sql, $connection); if(!$retval ) { $OutputMessage = 'RECORD DOES NOT ALLOW DELETION'; } $OutputMessage = 'RECORD DELETED'; mysql_close($connection); }//main else }//exception else } mysql_close($connection); } ?> 

Could I get a fresh look from someone?

What the others said, but also - your nesting is off. For example, the below nesting is not correct:

else
    { //exeption else       
        { //main else

You can't start the "main" there - the entire exception is wrapped in the main, or the main doesn't exist.

Either remove the starting and closing bracket for "main", or put a statement before it.

As per comments, apparently this is accepted/ignored in PHP. So replace my "can't" with "probably shouldn't, because it looks odd and serves no purpose".

$sql = "DELETE FROM `Kittenzz`
                        WHERE `KittenID`='".$_POST['KittenID']."'";

try this

You missed one single quote in your query.

 $sql = "DELETE FROM Kittenzz
         WHERE KittenID='$_POST[KittenID]";

Please try this :

 $sql = "DELETE FROM Kittenzz
         WHERE KittenID='".$_POST['KittenID']."';";

I think that you are missing the closing ' at the end of the delete sql. You also are not concatenating the $_POST[KittenID] variable but just sticking it at the end of the string. So:

$sql = "DELETE FROM Kittenzz
                    WHERE KittenID='$_POST[KittenID]";

becomes:

 $sql = "DELETE FROM Kittenzz
                    WHERE KittenID='" . $_POST[KittenID]. "'";

You have errors in your code:

First change this:

if ($_POST[KittenID]=='')

to this:

if ($_POST['KittenID']=='')

and this:

$sql = "DELETE FROM Kittenzz WHERE KittenID='$_POST[KittenID]";

to this:

$sql = "DELETE FROM Kittenzz WHERE KittenID='" . $_POST['KittenID'] . "'";

It seems that your query is missing a closing char \\'.

yours:

 "DELETE FROM Kittenzz WHERE KittenID='$_POST[KittenID] ";

shoud be:

 "DELETE FROM Kittenzz WHERE KittenID='$_POST[KittenID]' ";

BTW: @vivek code is more elegant.

Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM