Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP and checkbox array problem

Posted on 2012-03-21
3
Medium Priority
?
321 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 84

Accepted Solution

by:
Dave Baldwin earned 2000 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 84

Expert Comment

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

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
The viewer will learn how to count occurrences of each item in an array.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

722 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