doctortim Posted December 11, 2007 Share Posted December 11, 2007 Hi I am creating my two pages for removing records (articles) from my blog databases. These pages are delete.php and confirm_delete.php I can't really find why the code is getting rid of them from the database. Here is my php code in the confirm delete page... <?php include_once("include/connect_sql.inc.php"); $thisArticle_id = $_POST['article_id']; $sql = "SELECT * FROM blogprofessional WHERE article_id = '".$thisArticle_id."'"; $result = mysql_query($sql); $numberOfRows = mysql_num_rows($result); if ($numberOfRows==0) { echo 'Sorry. No records found !!'; } else if ($numberOfRows>0) { $i=0; $article_id = mysql_result($result,$i,"article_id"); $issue_no = mysql_result($result,$i,"issue_no"); $issue_date = mysql_result($result,$i,"issue_date"); $focus = mysql_result($result,$i,"focus"); $toc = mysql_result($result,$i,"toc"); $article = mysql_result($result,$i,"article"); } ?> and the confirm delete button/form is as follows: <tr><form name="customersEnterForm" method="POST" action="pro_blog_delete.php"> <td width="609"><input type="hidden" name="thisArticle_idField" value="<? echo $thisArticle_id; ?>" /></td> <td width="215"><input type="submit" name="submitConfirmDeleteCustomersForm" value="Delete from Blog Archive" /></td> <td width="85"><input type="button" name="cancel" value="Back" onclick="javascript:history.back();" /></td></form> </tr> now finally when the button is clicked and redirection to the delete page occurs this is the code that should process: <?php include_once("include/connect_sql.inc.php"); // Retreiving Form Elements from Form $thisArticle_id = addslashes($_POST['thisArticle_idField']); $sql = "DELETE FROM blogprofessional WHERE article_id = '$thisArticle_id'"; $result = mysql_query($sql); ?> When I check in phpmyadmin the record has not been deleted. Please help, thank you very much. Quote Link to comment Share on other sites More sharing options...
revraz Posted December 11, 2007 Share Posted December 11, 2007 Echo '$thisArticle_id' before you call the delete and see what it contains. Quote Link to comment Share on other sites More sharing options...
doctortim Posted December 11, 2007 Author Share Posted December 11, 2007 yep comes out empty Quote Link to comment Share on other sites More sharing options...
revraz Posted December 11, 2007 Share Posted December 11, 2007 So now you know why it doesn't delete. So you need to trace that variable down and see why it's blank. Quote Link to comment Share on other sites More sharing options...
doctortim Posted December 11, 2007 Author Share Posted December 11, 2007 I looked at the page source and found this for my confirm delete form: <tr> <form name="customersEnterForm" method="POST" action="pro_blog_delete.php"> <td width="609"><input type="hidden" name="thisArticle_idField" value="" /></td> <td width="215"><input type="submit" name="submitConfirmDeleteCustomersForm" value="Delete from Blog Archive" /></td> <td width="85"><input type="button" name="cancel" value="Back" onclick="javascript:history.back();" /></td> </form> </tr> The value is not getting the article id echoed into it. Once again, the php to insert the article id is follows <tr> <form name="customersEnterForm" method="POST" action="pro_blog_delete.php"> <td width="609"><input type="hidden" name="thisArticle_idField" value="<?php echo $article_id; ?>" /></td> <td width="215"><input type="submit" name="submitConfirmDeleteCustomersForm" value="Delete from Blog Archive" /></td> <td width="85"><input type="button" name="cancel" value="Back" onclick="javascript:history.back();" /></td> </form> </tr> I am also getting the "Sorry,no records found" message from the top part of my confirm delete page, which once again comes from this code: <?php include_once("include/connect_sql.inc.php"); $thisArticle_id = $_POST['article_id']; $sql = "SELECT * FROM blogprofessional WHERE article_id = '".$thisArticle_id."'"; $result = mysql_query($sql); $numberOfRows = mysql_num_rows($result); if ($numberOfRows==0) { echo 'Sorry. No records found !!'; } else if ($numberOfRows>0) { $i=0; $article_id = mysql_result($result,$i,"article_id"); $issue_no = mysql_result($result,$i,"issue_no"); $issue_date = mysql_result($result,$i,"issue_date"); $focus = mysql_result($result,$i,"focus"); $toc = mysql_result($result,$i,"toc"); $article = mysql_result($result,$i,"article"); } ?> Im stumped By the way this is the results of the error for the delete query Delete query: DELETE FROM blogprofessional WHERE article_id = ''Rows affected: 0 therefore the variable $thisArticle_id is not getting its value // Retreiving Form Elements from Form $thisArticle_id = addslashes($_POST['thisArticle_idField']); The argument for post is from 'name' in the confirm delete button. Though still doesn't appear to explain why the "no records" message appears on the confirm delete page. Quote Link to comment Share on other sites More sharing options...
revraz Posted December 11, 2007 Share Posted December 11, 2007 Where should this line get the ID from? <td width="609"><input type="hidden" name="thisArticle_idField" value="<?php echo $article_id; ?>" /></td> You are echoing $article_id into the form, yet nothing is setting $article_id to the actual row id. Quote Link to comment Share on other sites More sharing options...
doctortim Posted December 12, 2007 Author Share Posted December 12, 2007 yeah on my article listings page, each entry has a "delete" link next to it like this <a href="pro_blog_confirm_delete.php?article_id=<?php echo $row['article_id']; ?>">Delete Record</a> when you hover over the delete link the correct article id shows up in the bottom of the browser so I know that works. Then you link through to the confirm delete page where the header is as so: <?php include_once("include/connect_sql.inc.php"); $thisArticle_id = $_POST['article_id']; $sql = "SELECT * FROM blogprofessional WHERE article_id = '".$thisArticle_id."'"; $result = mysql_query($sql); $numberOfRows = mysql_num_rows($result); if ($numberOfRows==0) { echo 'Sorry. No records found !!'; } else if ($numberOfRows>0) { $i=0; $article_id = mysql_result($result,$i,"article_id"); $issue_no = mysql_result($result,$i,"issue_no"); $issue_date = mysql_result($result,$i,"issue_date"); $focus = mysql_result($result,$i,"focus"); $toc = mysql_result($result,$i,"toc"); $article = mysql_result($result,$i,"article"); } ?> thats where it is getting the id from, isn't this correct??!? Quote Link to comment Share on other sites More sharing options...
revraz Posted December 12, 2007 Share Posted December 12, 2007 You need to GET it, not POST it $thisArticle_id = $_GET['article_id']; Quote Link to comment Share on other sites More sharing options...
doctortim Posted December 12, 2007 Author Share Posted December 12, 2007 no that shouldn't matter, I have used the "post" method in the button that gets you to that code. This is driving me silly Quote Link to comment Share on other sites More sharing options...
mr_mind Posted December 12, 2007 Share Posted December 12, 2007 Try this, i think it is what you were trying to do. In here i am assuming that the article id is numeric and that the article id will be passed through the url, example: http://www.yourdomain.com/delete.php?article_id=1. all you need to worry about is making sure that ?article_id= is after your url. this combines all of your files into one to make it easier to maintain <?php include_once("include/connect_sql.inc.php"); if($_GET['article_id']) { if(is_numeric($_GET['article_id'])) { $article_id = $_GET['article_id']; $article_query = mysql_query("SELECT * FROM blogprofessional WHERE article_id='" . $article_id . "'"); $article_num = mysql_num_rows($article_query); if($numberOfRows > 0) { $article_array = mysql_fetch_array($article_query); $article_issue_num = $article_array['issue_no']; $article_issue_date = $article_array['issue_date']; $article_focus = $article_array['focus']; $article_toc = $article_array['toc']; $article = $article_array['article']; if($_POST['confirm']) { if(mysql_query("DELETE FROM blogprofessional WHERE article_id='" . $article_id . "'")) { print 'Blog deleted'; } else { print 'Failed connectiong to the server. Please notify an admin with this error: ' . mysql_error(); } } else { print '<form action=' . $_SERVER['PHP_SELF'] . '?article_id=' . $article_id . ' method=post>'; print '<table>'; print '<tr>'; print '<td width="215"><input type=submit name=confirm value="Delete from Blog Archive" /></td>'; print '<td width="85"><input type=button name=cancel value="Back" onclick="javascript:history.back();" /></td></form>'; print '</tr>'; print '</table>'; print '</form>'; } } else { print 'No articles found'; } } else { print 'No articles found'; } } else { print 'No articles found'; } ?> Quote Link to comment Share on other sites More sharing options...
doctortim Posted December 12, 2007 Author Share Posted December 12, 2007 I will try that, I haven't used print statements before any of my html, now I'm concerned. what does it do? Quote Link to comment Share on other sites More sharing options...
mr_mind Posted December 13, 2007 Share Posted December 13, 2007 i just put it in there basically it "prints" it to the screen. In that script though it will only print if the confirm button has not been pressed Quote Link to comment Share on other sites More sharing options...
revraz Posted December 13, 2007 Share Posted December 13, 2007 If you notice, he is also using $_GET. Quote Link to comment 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.