Jump to content

Form Checkboxes into MySQL


stevedav

Recommended Posts

greetings,

 

I have a form with many checkboxes, representing different locations. i am storing them into an array locations[]. My question is how do I get them into MySQL? A foreach loop? I'm sure this is basic for most. My brain is frozen on it. Thanks.

 

sd

Link to comment
Share on other sites

Try this

 

<input type='checkbox' name="check1[]" value=<?=$row1['V_ProspectID'];?>></td>

<!---CODE FOR STORING VALUE INTO DATABASE---->

if (is_array($_POST["check1"]))

{

     

while (list($key,$value) = each($_POST["check1"]))

{

//echo "$key - $value<BR>";

  $sql_update1="update ..... where V_ProspectID='$value'";

mysql_query($sql_update1)or die("Could not update  table");

            }

}

 

Link to comment
Share on other sites

Hi..

        you can store it in csv format in field value of sql. keep that field as text if u r having too much long text. or

you will have array in get,post or request. so you can serialize it and store it in to database. @ the time of using you can deserialize it..

     

 

Regards,

Vijay

Link to comment
Share on other sites

I'm assuming your have a strucutre of
<input type="checkbox" name="locations[]">
so lets do this
<?php
$string = "";
$i = 0;
foreach($_POST['locations'] as $value){
//Sets all commas after first
if($i != 0){echo ",";}
//If not empty set the mysql bool to true
if(!empty($value){
$string .= "Location ".$i." = 1";
}
else{
$string .= "Location ".$i." = 0;
}
$i++;
}
echo $string;
?>

That should produce a query string you can use assuming your sql table is structured like I have it written.  Then just query it and you all set.  If you are doing an insert it will be a tad different, but the idea is there

 

A note on suma's code, while it will work, it will be a resource hog running so many queries

Link to comment
Share on other sites

Putting data in comma sep lists not a good design, especially if it is a list of foreign keys.

 

To keep the data normalised, yet maintain a single query, use something like

<?php
if (isset($_GET['loc']))
{
    $id = $_GET['id'];
    foreach ($_GET['loc'] as $loc)
    {
        $sqlarr[] = "('$id', '$loc')";
    }
    $sql = "INSERT INTO table VALUES\n" . join(",\n", $sqlarr);
}

echo "<pre>$sql</pre>";       // execute the query here. Echo for demo purposes only
?>
<form>
    <input type="hidden" name="id" value="42">
    <input type="checkbox" name="loc[]" value="1"> loc 1<br/>
    <input type="checkbox" name="loc[]" value="2"> loc 2<br/>
    <input type="checkbox" name="loc[]" value="3"> loc 3<br/>
    <input type="submit" name="sub" value="Submit">
</form>

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.