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
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']);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.