Solved

PHP and checkbox array problem

Posted on 2012-03-21
3
315 Views
Last Modified: 2012-08-13
Hi again.

I have s slight problem with checkboxes being submitted and getting wrong values.

I have a form that pulls data dynamically from my db. Its basically data for a review system.
It will show me all Pending Reviews.

What I have is:
<form method="POST"><?php
while($custROW = mysql_fetch_array($custRLT))
{
echo '<input type="hidden" name="id[]" value="'.$custROW['id'].'">';
echo '<input type="checkbox" name="selected[]" value="1"  />';
echo $custROW['id'];
echo date('F j, Y',$custROW['reviewdateunix']);
echo $custROW['fname'].' '.$custROW['lname'];
echo $custROW['pname'];
echo truncate(strip_tags($custROW['review']), 60);
}
?>
</form>

Open in new window

The output look like this:

<form method="post">

<input type="hidden" name="id[]" value="18">
<input type="checkbox" name="selected[]" value="1"  />
18
March 21, 2012
Mr. Apple
Product 11
Great Product


<input type="hidden" name="id[]" value="17">
<input type="checkbox" name="selected[]" value="1"  />
17
March 21, 2012
Mr. Beans
Product 22
Best Product

etc .....
</form>

Open in new window


I use the check boxes trying to approve more than 1 review at any given time. I am passing the values to another file which contains the following code:

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {

		if(isset($_POST['id'])){	$id = $_POST['id'];}
		if(isset($_POST['selected'])){	$selected = $_POST['selected'];}
		
		for($i=0; $i<count($selected); $i++){
			
			if(strcmp($selected[$i],'1') == 0){
					$rstatus = 'approved';
					}else{
					$rstatus = 'pending';
					}
			$updSQL = mysql_query("UPDATE db_reviews 
				SET 
				rstatus = '$rstatus'
				WHERE 
				id = '$id[$i]' ") or die("Error: ".mysql_error());
		}

}	
?>

Open in new window


The error I am getting is that its only updating row starting from the top. If I would select 2 check boxes, it would update 2 rows, but not the ones by id, rather the first 2 ones.

Could anyone assist me with this problem?

Thanks you again
0
Comment
Question by:slickip
  • 2
3 Comments
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 37746189
Get rid of the hidden 'id' and put the id in the checkbox.  Unchecked 'check boxes' are not submitted so your code will be out of sequence on the unselected checkboxes.

echo '<input type="checkbox" name="selected[]" value="'.$custROW['id'].'">';

Open in new window


Then use a 'foreach' to go thru the 'selected' items submitted.
0
 

Author Closing Comment

by:slickip
ID: 37748070
Thank you!!

I was just about to jump off my balcony (Ground floor) ... =D. Works perfect. Exactly what I wanted.

Best Regards.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 37748747
You're welcome.!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question