green917 Posted December 19, 2009 Share Posted December 19, 2009 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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/185673-my-delete-function-isnt-working/ Share on other sites More sharing options...
PravinS Posted December 19, 2009 Share Posted December 19, 2009 Your delete query is wrong, you have used $ in where condition for field name. Use below given query. $query = "DELETE FROM recipes WHERE id = '$id'"; Quote Link to comment https://forums.phpfreaks.com/topic/185673-my-delete-function-isnt-working/#findComment-980429 Share on other sites More sharing options...
abazoskib Posted December 19, 2009 Share Posted December 19, 2009 To add to that, you never even define $id. Quote Link to comment https://forums.phpfreaks.com/topic/185673-my-delete-function-isnt-working/#findComment-980431 Share on other sites More sharing options...
green917 Posted December 19, 2009 Author Share Posted December 19, 2009 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/185673-my-delete-function-isnt-working/#findComment-980436 Share on other sites More sharing options...
PravinS Posted December 19, 2009 Share Posted December 19, 2009 How will you get $row['id']? Quote Link to comment https://forums.phpfreaks.com/topic/185673-my-delete-function-isnt-working/#findComment-980440 Share on other sites More sharing options...
abazoskib Posted December 19, 2009 Share Posted December 19, 2009 you need to pass the actual id in a hidden input. then define the variable in a way like $id = $_POST['hidden_id'] after the form is submitted Quote Link to comment https://forums.phpfreaks.com/topic/185673-my-delete-function-isnt-working/#findComment-980441 Share on other sites More sharing options...
green917 Posted December 19, 2009 Author Share Posted December 19, 2009 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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/185673-my-delete-function-isnt-working/#findComment-980444 Share on other sites More sharing options...
premiso Posted December 19, 2009 Share Posted December 19, 2009 $del = 'DELETE FROM recipes WHERE id="$id"'; That is invalid SQL, in MySQL you must use the single quotes around data. $del = "DELETE FROM recipes WHERE id='$id'"; Try that and see if it works. Quote Link to comment https://forums.phpfreaks.com/topic/185673-my-delete-function-isnt-working/#findComment-980451 Share on other sites More sharing options...
mrMarcus Posted December 19, 2009 Share Posted December 19, 2009 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'] . '" /> Quote Link to comment https://forums.phpfreaks.com/topic/185673-my-delete-function-isnt-working/#findComment-980503 Share on other sites More sharing options...
abazoskib Posted December 19, 2009 Share Posted December 19, 2009 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']}" />' Quote Link to comment https://forums.phpfreaks.com/topic/185673-my-delete-function-isnt-working/#findComment-980583 Share on other sites More sharing options...
premiso Posted December 19, 2009 Share Posted December 19, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/185673-my-delete-function-isnt-working/#findComment-980589 Share on other sites More sharing options...
mrMarcus Posted December 19, 2009 Share Posted December 19, 2009 You should really not get into the habit of echo'ing HTML ... and why is that? Quote Link to comment https://forums.phpfreaks.com/topic/185673-my-delete-function-isnt-working/#findComment-980645 Share on other sites More sharing options...
George Botley Posted December 19, 2009 Share Posted December 19, 2009 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.. Quote Link to comment https://forums.phpfreaks.com/topic/185673-my-delete-function-isnt-working/#findComment-980646 Share on other sites More sharing options...
mrMarcus Posted December 20, 2009 Share Posted December 20, 2009 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). Quote Link to comment https://forums.phpfreaks.com/topic/185673-my-delete-function-isnt-working/#findComment-980693 Share on other sites More sharing options...
abazoskib Posted December 20, 2009 Share Posted December 20, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/185673-my-delete-function-isnt-working/#findComment-980694 Share on other sites More sharing options...
abazoskib Posted December 20, 2009 Share Posted December 20, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/185673-my-delete-function-isnt-working/#findComment-980697 Share on other sites More sharing options...
green917 Posted December 22, 2009 Author Share Posted December 22, 2009 Thank you all so much for all of your help. This solved my issue and taught me some important syntax lessons about php I didn't know before. I really appreciate the help. Green Quote Link to comment https://forums.phpfreaks.com/topic/185673-my-delete-function-isnt-working/#findComment-982577 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.