Jump to content

Update not working in foreach loop?!


jarv

Recommended Posts

Please help,

 

I have 6 checkboxes in an array 0|1|2|3|4|5

 

I need to be able to UPDATE my database based on which checkboxes are checked, if it's the last one then I don't want to add a leading '|'

 

here is my code so far:

 

if(sizeof($_POST['hear'])) {
foreach($_POST['hear'] AS $heard){
		$heard = $heard.'|';
		if($heard == '5'){
		$heard = $heard;
		}
} //end foreach
} //end IF
$sql1 = "UPDATE aarbookts_booking_bookings SET how='$heard' WHERE id = '$id'";
	$result1 = mysql_query($sql1,$link) or die('Error: ' . mysql_error() . '<br>SQL: ' . $sql1);

 

Please help?!

Link to comment
https://forums.phpfreaks.com/topic/243866-update-not-working-in-foreach-loop/
Share on other sites

You *should* ideally  be using an associative table with individual records for each value. Alternatively you could use a binary value where each bit represents a 0 or 1. E.g. the number "13 is equivalent to the binary value of "01101" which would represent 5=false, 4=true, 3=true, 2=false, 1=true. This gives you the ability to query the records based upon certain values being set or not using bit-wise operators.

 

However, if you really are determine to store multiple values as a single string, then you simply need to use implode.

 

Replace all of this

foreach($_POST['hear'] AS $heard){
    $heard = $heard.'|';
    if($heard == '5'){
        $heard = $heard;
    }
} //end foreach

 

With just this

$heard = implode('|', $_POST['hear']);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.