Reputation: 188
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
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
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
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
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
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
Reputation: 2447
$sql = "DELETE FROM `Kittenzz`
WHERE `KittenID`='".$_POST['KittenID']."'";
try this
Upvotes: 0