PHP Form and While Loop

I have a comment system that loads the db, checks the profile page your on, then checks the database for the same username. If they match it echos the comment. This is all done in a while loop. Im trying to now create a ‘Remove Comment’ feature but im having some issues. Im guessing because its a while loop it doesnt know what one you are talking about. Ive tried making the submit button have same ID as the comment so they correspond and it knows what to remove but that doesnt work.

Ive added an echo to see if when i click Remove comment if it does anything and nothing does.

Here is my code:

[code] <?php

					$connect = mysql_connect("localhost","airsoftg_admin","pimpon316") or die("couldnt connect!");
					mysql_select_db("airsoftg_news") or die("Couldnt find DB!");
					$result=mysql_query("SELECT * FROM comments");

					$num=mysql_numrows($result);

					mysql_close($connect);
					$min = $num - 10;
					$i = $num - 1;

					echo "<form action='profile.php?username=<?=$pdbusername?>' method='POST'>";

					while ($num > $min && $num > 0)
					{
						$comment = mysql_result($result,$i,"comment");
						$commentuser = mysql_result($result,$i,"commentuser");
						$commentdate = mysql_result($result,$i,"commentdate");
						$commentto = mysql_result($result,$i,"commentto");
						$commentid = mysql_result($result,$i,"commentid");

							if(strtolower($pdbusername)==strtolower($commentto))
							{
								echo "<div>";
								echo "<div class='fl imgborder' style='padding:10px;'>";
								$connect = mysql_connect("localhost","airsoftg_admin","pimpon316") or die("couldnt connect!");
								mysql_select_db("airsoftg_login") or die("Couldnt find DB!");
								$query = mysql_query("SELECT * FROM users WHERE username='$commentuser'");
															
								$numrows = mysql_num_rows($query);

								mysql_close($connect);

								if ($numrows!=0)
								{

									echo "<div class='fr' style='padding:10px;'>
									<div class='news02'>
										<input type='submit' name='remove$commentid' value='Remove Comment' class='removecomment'/>
									</div>
									</div>";
								}

							}
						$num--;
						$i--;
					}

					echo "</form>";

				$remove = $_POST['remove$commentid'];
				if($remove)
				{
				mysql_query("DELETE FROM comments WHERE commentid='$commentid'");
				}


				?>[/code]

Probably doesn’t make much sense but its the best i can explain it. If anyone wants to Team Viewer, ill be willing to do that.

First, you do not need to connect to mysql and select db EVERY time when you want to query database. So, remove this from the loop. And also remove all instances of mysql_close($connect) - thsi is not really needed. But if you wish, you can leave this at bottom of your script (but not within loop, or before loop - becaus this not make sense).
Now regarding your question. I suggest you to have your button set like this within your loop:

<input type='submit' name='remove[$commentid]' value='Remove Comment' class='removecomment'/>

Then, to remove comment you can do this:
[php] $remove = $_POST[‘remove’];
if(is_array($remove) foreach($remove as $commentid=>$rm){
mysql_query(“DELETE FROM comments WHERE commentid=’$commentid’”);
}[/php]

When i try this i get error
Parse error: syntax error, unexpected T_FOREACH in /home/airsoftg/public_html/profile.php on line 323

I tried googling it, and watched some videos on youtube but still couldnt get it.

is name=‘remove[$commentid]’ able to get $remove = $_POST[‘remove’]; wouldnt the $_POST[’’]; need the comment id also?

Also what is var $rm?

Thanks

I missed one closing ) that’s why you get this error. Here is fixed version:
[php]
$remove = $_POST[‘remove’];
if(is_array($remove)) foreach($remove as $commentid=>$rm){
mysql_query(“DELETE FROM comments WHERE commentid=’$commentid’”);
}
[/php]
You can try debugging and you will see what each posted variable is. Add this code to debug:
[php]
echo ‘

’;
print_r($_POST);
echo ‘
’;
[/php]
This will show you all the posted variables, arrays, indexes and values.

I saw that the line was missing a ), but i put it at the end of the line.

I tried the print_r thing but all it does is print:

Array ( )

Try to hit your form’s submit button and see what print_r($_POST) show you.

basically it just refreshes the page. Nothing happens still. I was wondering if $_POST[‘remove’] even gets name=‘remove$commentid’.

Post remove is looking for an input with the name remove, not remove1, or remove2… ect. Am i correct?

I do not know what is your entire code, but try this simple file. If it not displaying anything - this probably means you have no php installed.
test.php[php]

<?php echo 'Test 1
';
  print_r($_POST);
  echo '
';

echo ‘Test 2
’;
foreach($_POST[‘remove’] as $key=>$value){
echo ‘remove[’.$key.’] = ‘.$value.’
’;
}
?>

Field 1:
Field 2:
Field 3:
[/php]

I have PHP installed. I made login scripts, register scripts, team scripts, profile pages, a bulletin board, comments. I just cant get this to work for some bizarre reason.

Sorry for double post. I decided ill try the test thing.

here are my results.

When i go to test.php i get

Under Test 1:

Array ( )

Under Test 2:
Nothing is returned

Then i get an error

Warning: Invalid argument supplied for foreach() in /home/airsoftg/public_html/test.php on line 10

Then the fields come up.

If i enter data

Field 1 = hello Field 2 = how are you Field 3 = bye

When i Submit
Under Test 1:

Array ( [remove] => Array ( [1] => hello [2] => how are you [3] => bye )
[go] => Go

)

Under Test 2:

remove[1] = hello remove[2] = how are you remove[3] = bye

and the error disappears

This is the correct result. I did meant this sample code for you to test with POST method. Did you learn anything from the code, I think it answered your question:

I was wondering if $_POST['remove'] even gets name='remove$commentid'.

If you want sample code without warning message, just verify if $_POST[‘remove’] array is defined before using it in foreach:
[php]
if(is_array($_POST[‘remove’])) foreach($_POST[‘remove’] as $key=>$value){
echo ‘remove[’.$key.’] = ‘.$value.’
’;
}
[/php]

Thank you. I was in US History class today thinking about things (cause it was really boring class) and i realized a better way to use this. Great site and great help. Thanks

Kay, my thing i wanted to try kinda failed, but i got it by changing the Test Code. Thank you for helping me get back on track. Great new code to know

Sponsor our Newsletter | Privacy Policy | Terms of Service