Pursuer of Dying Stars
Pursuer of Dying Stars

Reputation: 188

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?

Upvotes: 0

Views: 142

Answers (6)

Mackan
Mackan

Reputation: 6271

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".

Upvotes: 2

YyYo
YyYo

Reputation: 641

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

Upvotes: 0

AkshayP
AkshayP

Reputation: 2169

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']."';";

Upvotes: 0

Adrian Cid Almaguer
Adrian Cid Almaguer

Reputation: 7791

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'] . "'";

Upvotes: 0

Daniel Casserly
Daniel Casserly

Reputation: 3543

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]. "'";

Upvotes: 0

Vivek Singh
Vivek Singh

Reputation: 2447

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

try this

Upvotes: 0

Related Questions