rawky1976 Posted December 3, 2007 Share Posted December 3, 2007 Hello all I'm having a little trouble with the code below. I think it's either that the braces are misplaced or the statements are in the wrong order. In the form if I echo'$recno' then I get the number that was passed from the previous page, but when I change it to echo'$dn' it's just blank. Is the variable not in scope of the form? Also when the form is submitted the message that the record has been deleted is displayed, although it hasn't actually been deleted? Thanks for any help you can offer, Mark <h2>Confirm Record Delete</h2> <?php if (isset($_GET['recno'])) { $recno = (int) $_GET['recno']; } include 'library/config.php'; include 'library/opendb.php'; $query = "select * from document WHERE document_id = '$recno'"; $result = mysql_db_query($query); if ($result) { $r = mysql_fetch_array($result); $dn = $r["document_name"]; } if (isset($_POST['submitted'])) { $query = "DELETE FROM document WHERE document_id=$recno"; if (mysql_num_rows($result) == 1) { $result = mysql_query($query); } echo 'The record was sucessfully removed'; include ('./library/cpfooter.html'); exit(); } ?> <form action="confirmdelete.php" method="post"> <fieldset><legend>Confirm Record Deletion</legend> <p>Are you sure you wish to remove the record for the document: -<br /><br /> <?php echo "$dn"; ?><br /><br /> <input type="submit" value="Yes Delete Record"><input type="hidden" name="submitted" value="TRUE" /> </fieldset> </form> Quote Link to comment Share on other sites More sharing options...
~n[EO]n~ Posted December 3, 2007 Share Posted December 3, 2007 Try this hope it works... <?php include 'library/config.php'; include 'library/opendb.php'; // check if rec no is set or not... if (isset($_GET['recno'])) { $recno = (int) $_GET['recno']; } //query $query = "select * from document WHERE document_id = '$recno'"; $result = mysql_query($query) or die(mysql_error()); // if result found if ($result) { $r = mysql_fetch_array($result); $dn = $r['document_name']; } // from here you are trying to delete the record... if (isset($_POST['submitted'])) { $query_del = "DELETE FROM document WHERE document_id=$recno"; $result_del = mysql_query($query_del); echo 'The record was sucessfully removed'; include ('./library/cpfooter.html'); exit(); } ?> <h2>Confirm Record Delete</h2> <form action="confirmdelete.php" method="post"> <fieldset><legend>Confirm Record Deletion</legend> <p>Are you sure you wish to remove the record for the document: -<br /><br /> <?php echo "$dn"; ?><br /><br /> <input type="submit" value="Yes Delete Record"><input type="hidden" name="submitted" value="TRUE" /> </fieldset> </form> Quote Link to comment Share on other sites More sharing options...
rawky1976 Posted December 3, 2007 Author Share Posted December 3, 2007 Hello, thanks for the response. I now get the filename displayed on the confirm delete page ($dn is being assigned and displayed). The delete success message is displayed but the record still isn't actually deleted. I'll double check the fieldnames etc. Mark Quote Link to comment Share on other sites More sharing options...
Bramme Posted December 3, 2007 Share Posted December 3, 2007 n~ link=topic=170348.msg752741#msg752741 date=1196694992] if (isset($_POST['submitted'])) <input type="submit" value="Yes Delete Record"><input type="hidden" name="submitted" value="TRUE" /> [/quote] I think you could safely replace those two with [code] if(isset($_POST['submDel'])) { <input type="submit" value="Yes Delete Record" name="submDel" /> That's what I always do anyway...[/code] Quote Link to comment Share on other sites More sharing options...
rawky1976 Posted December 3, 2007 Author Share Posted December 3, 2007 Thanks, I think the code I have for that bit does work though, I've used it throughout the application (got it from a book!) Quote Link to comment Share on other sites More sharing options...
rawky1976 Posted December 3, 2007 Author Share Posted December 3, 2007 This is strange, I've ran the query through the Query Browser and it deletes the record. It can only be the variable and that is used successfully in the SELECT statement above? Quote Link to comment Share on other sites More sharing options...
rawky1976 Posted December 3, 2007 Author Share Posted December 3, 2007 I just thought, running in Query Browser is from the MySQL root login right? Does the application run under some other login that doesn't have permission to delete? Would that show up in the 'or die'? Quote Link to comment Share on other sites More sharing options...
rawky1976 Posted December 3, 2007 Author Share Posted December 3, 2007 Hello again! I've been looking into this and I think I've discovered the problem. The recno value comes as a GET from the previous page. When the form is submitted it POSTS, so the recno doesn't get populated when it reloads. I've therefore updated the script as below. I've used another hidden field in the form and tried to assign the recno value to it for the post. I've also added an elseif at the top to catch both GET and POST. I'm not sure whether you can do it that way though? Is the syntax correct for my new hidden field? <?php include 'library/config.php'; include 'library/opendb.php'; // check if rec no is set or not... if (isset($_GET['recno'])) { //$recno = (int) $_GET['recno']; $recno = $_GET['recno']; } elseif (isset($_POST['recno'])){ $recno = $_POST['recno']; } // from here you are trying to delete the record... if (isset($_POST['submitted'])) { $query = "DELETE FROM knowledgebase.document WHERE document_id='$recno'"; $result = mysql_query($query) or die(mysql_error()); echo 'The record was sucessfully removed'; include ('./library/cpfooter.html'); exit(); } //query $query_sel = "select * from document WHERE document_id = '$recno'"; $result_sel = mysql_query($query_sel) or die(mysql_error()); // if result found if ($result_sel) { $r = mysql_fetch_array($result_sel); $dn = $r['document_name']; } ?> <form action="confirmdelete.php" method="post"> <fieldset><legend>Confirm Record Deletion</legend> <p>Are you sure you wish to remove the record for the document: -<br /> <?php echo "$dn"; ?><br /><br /> <input type="submit" value="Yes Delete Record"><input type="hidden" name="submitted" value="TRUE" /><input type="hidden" name="recno" value="$recno"> </fieldset> </form> 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.