Jump to content

Form in While Loop

Go to solution Solved by mac_gyver,

Recommended Posts

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:

	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>

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:


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.
		// 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() {


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,


Link to comment
Share on other sites

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.

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.

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.