Nightasy Posted March 26, 2014 Share Posted March 26, 2014 Greetings all, I have been developing a comment box for my site and I ran into an issue I can't seem to figure out. In my comment box when I am logged in as an admin I want to be able to delete comments by simply clicking a button. The comments are printed out from a database using a while loop: <?php echo '<div id="commentMainTitle"><h3>Video Comments</h3></div>'; while($row = mysqli_fetch_array($results, MYSQLI_ASSOC)){ echo '<div id="commentUniqueWrap"> <div id="commentUniqueTitleMessageWrap"> <div id="commentUniqueTitle"> <h4>'. stripslashes($row['name']) .'</h4>';?> <?php if (isset($_SESSION['admin'])){?> <!--If admin add form for delete comment.--> <form enctype="multipart/form-data" action="includes/function/deletecomment.php" method="post"> <input type="hidden" name="cursect" value="<?php echo $sect1;?>" /> <input type="hidden" name="curvideored" value="<?php echo $_GET['curvideo'];?>" /> <input type="hidden" name="postID" value="<?php echo $row['id'];?>" /> <input type="submit" name="submit" value="Delete" /> <?php echo $row['id']; } echo '</div> <div id="commentUniqueMess"> <p>' . stripslashes($row['comment']). '</p> <p class="datealign">' . $row['date'] . '</p> </div> </div> </div>'; } ?> When the comments are listed out a Delete button is listed next to the commentors name. A comment is deleted however it is not the comment that is selected for deletion. Instead the last comment that was echo'd out from the while loop is deleted. Here is the script that deletes the comment: <?php if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['postID'])){ //Requires the database connection script. require $_SERVER['DOCUMENT_ROOT'] . '/mywebsite/masters/connect.php'; $qdelete = "DELETE FROM comment WHERE id ='".$_POST['postID']."' LIMIT 1"; if (!mysqli_query($connect,$qdelete)){ die('Error: ' . mysqli_error($connect)); } // Close database connection. mysqli_close($connect); // Set path for redirect. $redirect = 'http://www.mywebsite.com/myvideos.php?sect=' . $_POST['cursect'] . '&curvideo=' . $_POST['curvideored']; // Redirect to previous page. echo '<script type="text/javascript"> setTimeout(redirect, 0000); function redirect() { location.href="'.$redirect.'" } </script>'; } ?> I'm not sure what the issue is or how to go about solving it. I feel like the answer is staring me in the face but I've been looking at the code for too long and it has me at a loss. Any help would be greatly appreciated. Thank you for your time. Best Regards, Nightasy Quote Link to comment https://forums.phpfreaks.com/topic/287278-form-in-while-loop/ Share on other sites More sharing options...
Solution mac_gyver Posted March 26, 2014 Solution Share Posted March 26, 2014 your form doesn't have a closing </form> tag, that i could see, so you actually have just one huge form and the last value for any name='...' attribute is what will be submitted. make sure your html markup is complete and valid. Quote Link to comment https://forums.phpfreaks.com/topic/287278-form-in-while-loop/#findComment-1473920 Share on other sites More sharing options...
Nightasy Posted March 26, 2014 Author Share Posted March 26, 2014 your form doesn't have a closing </form> tag, that i could see, so you actually have just one huge form and the last value for any name='...' attribute is what will be submitted. make sure your html markup is complete and valid. That was it. See, I knew it was something simple and staring me right in the face. Sometimes you just need a fresh pair of eyes to point out a blatant mistake. Thank you very much. Quote Link to comment https://forums.phpfreaks.com/topic/287278-form-in-while-loop/#findComment-1473930 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.