Jump to content

my delete function isn't working


green917

Recommended Posts

Hi all,

 

I have a basic table for the site I'm writing that has a delete function for each record that should delete the record from the database. When you click the "delete" button, it appears to executes the command but, when I return to the contents table via the link I coded in, the record I was attempting to delete is still there. I have a feeling I may be calling the variable incorrectly in the mysql query called $query. The code follows. Any help would be greatly appreciated. Thank you in advance.

 

<?php

include 'config.php';

if ($_POST['delete']) {
$query = "DELETE FROM recipes WHERE $id = '$id'";
$result = mysql_query($query,$link);

echo 'Recipe has been deleted';
echo '<br /><br /><a href="/hlgcon.php">Back to database contents</a>';
}
else{

$sql = "SELECT id,title FROM recipes ORDER BY title";
$result = mysql_query($sql,$link);

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link rel="stylesheet" href="concss.css" type="text/css">
</head>

<body>


<form name="recitem" action="<?php echo $PHP_SELF ?>" method="post">
<table width="650" border="1" cellpadding="2" cellspacing="1">
<tr><th width="100%" colspan="3" style="background-color:#cddf77;"><center>Recipes</center></th></tr>
<tr><th>id</th><th>title</th><th>delete?</th></tr>

<?php
while($row = mysql_fetch_array($result)) {
echo '<tr><td>'.$row['id'].'</td>';
echo '<td>'.$row['title'].'</td>';
echo '<td><center><input type="submit" name="delete" value="delete" /></center></td>';
echo '</tr>';
}
echo '<TR><td width="100%" height="30" valign="center" colspan="3"><center><a href="/hlgup.php"><img src="recipebtn.gif" border="0"></a></center></td></TR>'; 

mysql_close($link);

?>	
</table>
</form>

</body>
</html>
<?php
}
?>

Link to comment
Share on other sites

Thanks for the help but, after changing the $query and defining $id as

 

$id = $row['id'];

 

prior to the aforementioned $query, the function still isn't working. Any more help you can provide me is greatly appreciated as, obviously, I'm rather new to this stuff.

 

if (isset ($_POST['delete'])) {

include 'config.php';

$id = $row['id'];

$query = 'DELETE FROM recipes WHERE id="$id"';
$result = mysql_query($query,$link);

echo 'Recipe has been deleted';
echo '<br /><br /><a href="/hlgcon.php">Back to database contents</a>';
}
else{

include 'config.php';

$sql = "SELECT id,title FROM recipes ORDER BY title";
$result = mysql_query($sql,$link);

?>

Link to comment
Share on other sites

Sorry for not getting this but, I still can't get it to delete the record. I updated the code by inserting a hidden input named "hidden_id" and passed it in a variable before establishing $query. The code is below. Thanks for all of your help again

 

<?php


if (isset ($_POST['delete'])) {

include 'config.php';

$id = $_POST['hidden_id'];

$del = 'DELETE FROM recipes WHERE id="$id"';
$result = mysql_query($del,$link);

echo 'Recipe has been deleted';
echo '<br /><br /><a href="/hlgcon.php">Back to database contents</a>';
}
else{

include 'config.php';

$sql = "SELECT id,title FROM recipes ORDER BY title";
$result = mysql_query($sql,$link);

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link rel="stylesheet" href="concss.css" type="text/css">
</head>

<body>


<form name="recitem" action="<?php echo $PHP_SELF ?>" method="post">
<table width="650" border="1" cellpadding="2" cellspacing="1">
<tr><th width="100%" colspan="4" style="background-color:#cddf77;"><center>Recipes</center></th></tr>
<tr><th>id</th><th>title</th><th>delete?</th></tr>

<?php
while($row = mysql_fetch_array($result)) {
echo '<tr><td>'.$row['id'].'</td>';
echo '<td>'.$row['title'].'</td>';
echo '<td><center><input type="hidden" name="hidden_id" value="$row[id]" /><input type="submit" name="delete" value="delete" /></center></td>';
echo '</tr>';
}
echo '<TR><td width="100%" height="30" valign="center" colspan="4"><center><a href="/hlgup.php"><img src="recipebtn.gif" border="0"></a></center></td></TR>'; 

mysql_close($link);

?>	
</table>
</form>

</body>
</html>
<?php
}
?>

Link to comment
Share on other sites

as well, you cannot encapsulate php variables in single-quotes .. well, you can, they just won't be parsed.

 

this line:

echo '<td><center><input type="hidden" name="hidden_id" value="$row[id]" /> ...

 

must concatenate like so:

 

echo '<td><center><input type="hidden" name="hidden_id" value="' . $row['id'] . '" />

Link to comment
Share on other sites

as well, you cannot encapsulate php variables in single-quotes .. well, you can, they just won't be parsed.

 

this line:

echo '<td><center><input type="hidden" name="hidden_id" value="$row[id]" /> ...

 

must concatenate like so:

 

echo '<td><center><input type="hidden" name="hidden_id" value="' . $row['id'] . '" />

 

You should really not get into the habit of echo'ing HTML, but another way to do it which has recently become my favorite:

 

echo '<td><center><input type="hidden" name="hidden_id" value="{$row['id']}" />'

 

Link to comment
Share on other sites

echo '<td><center><input type="hidden" name="hidden_id" value="{$row['id']}" />'

 

 

That code won't work because of the single quotes the { } will only be interepted inside of double quotes:

echo "<td><center><input type='hidden' name='hidden_id' value='{$row['id']}' >";

 

Would be the proper way.

Link to comment
Share on other sites

Well,

 

It really is upto personal preference - but I have had cases where it uses resources on a server whilst parsing and echoing the HTML.

 

My recommendation for any echoing would be something similar to this:

 

<?

while($row=mysql_fetch_array($query)) {
//SOME CODE HERE THAT GETS DATA

?>

//SEE BELOW

<? } ?>

 

You may write pure HTML in the middle section as you have escape the PHP tag... Rest assured, the browser will still parse the PHP and still think that the HTML is within the PHP. This way, you can run if statements and loops etc whilst writing HTML within them without worrying about escaping values.

 

If you had a variable that you wanted called within the HTML you were writing simply restart a PHP tag and echo the variable like so <? echo "$name"; ?> - this would then echo the previously set variable into the pure HTML..

 

Link to comment
Share on other sites

my apologies.  i was being sarcastic.

 

the difference in processing time is near non-existent.  you're talking hundredths, even thousandths of milliseconds on some pretty heavy scripts (just ran a loop @ 10,000 iterations, and while there was a difference, both executed in under 0.04 of a second).

Link to comment
Share on other sites

You should really not get into the habit of echo'ing HTML ...

 

and why is that?

 

You waste time/resources doing it. A good example is when I put together a table on a webpage listing over 1000 rows from a mysql table. I quickly threw it together and echo'ed the HTML. It was so darn slow. I simply placed the HTML outside of the PHP blocks, and I noticed much better speed when loading the table. I don't have an official benchmark for it, but no one has complained since. Also, it makes debugging much harder. Keep HTML as HTML, and PHP as PHP. You'll thank yourself when you have to go back weeks later to fix a bug.

Link to comment
Share on other sites

my apologies.  i was being sarcastic.

 

the difference in processing time is near non-existent.  you're talking hundredths, even thousandths of milliseconds on some pretty heavy scripts (just ran a loop @ 10,000 iterations, and while there was a difference, both executed in under 0.04 of a second).

 

Maybe, but I'm positive that when viewing a webpage, loading times are a lot faster when you do not echo full HTML. Like I said it was very noticeable for myself and others that use my website.

 

I just re-tested it, with the old and new versions of the page, and its remarkably faster when just echo'ing the values , and not full HTML code.

Link to comment
Share on other sites

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.