Jump to content

Form Checkboxes into MySQL


stevedav

Recommended Posts

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");

            }

}

 

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

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

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>

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.