Solved

PHP and checkbox array problem

Posted on 2012-03-21
3
316 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
php hashing methods 3 50
CSV export single column not inserting comma's in file 11 69
e commerce steps shown instead of self testing 2 59
SSL unsecure page mystery 17 47
Creating and Managing Databases with phpMyAdmin in cPanel.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

752 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