PDA

View Full Version : Deleting from MySQL in PHP


loxinthe
January 2nd, 2008, 06:10
I have two files here to delete a record form a database, first:

<?
mysql_connect(localhost,$uname,$pword);
@mysql_select_db($db) or die( "Unable to select database");
$query="SELECT * FROM special";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

echo "<b><center>Database Output</center></b><br><br>";

$i=0;

echo "<table>";

while ($i < $num) {

$title=mysql_result($result,$i,"title");
$pic=mysql_result($result,$i,"pic");
$description=mysql_result($result,$i,"description");
$expire=mysql_result($result,$i,"expire");
$valid=mysql_result($result,$i,"valid");


echo "<tr><td><b>$title</b></td>
<td rowspan='5'><img src=$pic></td></tr>
<tr><td>Description: $description</td></tr>
<tr><td>Expiration Date: $expire</td></tr>
<tr><td>Valid with other offers?: $valid</td></tr>";


echo "<tr><td colspan='2'><form ENCTYPE='multipart/form-data' method='post' action='editspecial.php'>
<input type='hidden' name='title' value=$title>
<input type='hidden' name='pic' value=$pic>
<input type='hidden' name='description' value=$description>
<input type='hidden' name='expire' value=$expire>
<input type='hidden' name='valid' value=$valid>
<input type='submit' value='Edit Special'>
</form>

<form ENCTYPE='multipart/form-data' method='post' action='deletespecial.php'>
<input type='hidden' name='title' value=$title>
<input type='hidden' name='pic' value=$pic>
<input type='hidden' name='description' value=$description>
<input type='hidden' name='expire' value=$expire>
<input type='hidden' name='valid' value=$valid>
<input type='submit' value='Delete Special'>
</form>

</td>
</tr>
<tr>
<td colspan='2'>
<br<hr><br>
</td>
</tr>";

$i++;
}
echo "</table>";
?>


Now here is deletespecial.php that the last files refers to when they click delete:

<?
session_start();
if(!session_is_registered(myusername)){
header("location:main_login.php");
}
$uname="<<removed>>";
$pword="<<removed>>";
$db="<<removed>>";




$title=$_POST['title'];
$pic=$_POST['pic'];
$description=$_POST['description'];
$expire=$_POST['expire'];
$valid=$_POST['valid'];
mysql_connect(localhost,$uname,$pword) or die("It's gone");
@mysql_select_db($db) or die( "Unable to select database");


$query = "DELETE FROM special WHERE title='$title',pic='$pic',description='$descriptio n',expire='$expire',valid='$valid'" or die ("It is still there!");








?>

<div align="center"><b>Record has been successfully deleted.</b></div>


I always get Record has been successfully deleted, but it isn't. Please Help.

the_pm
January 2nd, 2008, 06:38
My limited PHP knowledge is good for one answer and one educated guess:

1. You're getting the "Record has been successfully deleted." message because it is programmed to show up so long as the query doesn't die.

2. It doesn't look like the query is actually being run, which would account for why you're getting the message and not a failure message (or an actual successful deletion).

Confirmation from someone else please!

inimino
January 2nd, 2008, 07:03
2. It doesn't look like the query is actually being run, which would account for why you're getting the message and not a failure message (or an actual successful deletion).

Quite right. It helps to run the query against the database, all the posted code does is assign it to a variable.

loxinthe
January 2nd, 2008, 07:42
lol

I don't believe it was that simple of an oversight. Thanks to all.

You are awesome, I needed that extra set of eyes.

lox

loxinthe
January 2nd, 2008, 08:08
Ok guys here is how I changed the code above to run the query, and now I still get the same thing. I have checked the table name, and filed names just to be sure, but think that would ahve sotpped the script altogether. Still It doesn't actually delete anything though it seems to run correctly.

deletespecial.php:

<?
session_start();
if(!session_is_registered(myusername)){
header("location:adminlogin.php");
}
$uname=***********;
$pword=***********;
$db=***********;




$title=$_POST['title'];
$pic=$_POST['pic'];
$description=$_POST['description'];
$expire=$_POST['expire'];
$valid=$_POST['valid'];
mysql_connect(localhost,$uname,$pword) or die("It's gone");
@mysql_select_db($db) or die( "Unable to select database");


$query = "DELETE FROM special WHERE

title='$title',pic='$pic',description='$descriptio n',expire

='$expire',valid='$valid'" or die ("It is still there!");
mysql_query($query);

mysql_close();





?>

<div align="center"><b>Record has been successfully

deleted.</b></div>

loxinthe
January 2nd, 2008, 09:27
ok I fixed the problem by using a primary key field of id that is hidden and auto increments Like I should have been doing all along.

Thanks again.

the_pm
January 2nd, 2008, 14:31
ok I fixed the problem by using a primary key field of id that is hidden and auto increments Like I should have been doing all along.

Thanks again.You beat me to it. I was going to point out that the script didn't seem to be targeting a particular record :)