Solved

PHP and checkbox array problem

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
RDBMS and No sql database 4 38
Facial recognition to look through the whole database for a person 3 40
updating the date data 12 20
echo time from sql to input type="time" 9 17
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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…

919 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

20 Experts available now in Live!

Get 1:1 Help Now