Solved

PHP and checkbox array problem

Posted on 2012-03-21
3
311 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 82

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 82

Expert Comment

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
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.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now