Page 1 of 1

inserting checkbox data into database

Posted: Mon Jul 13, 2009 6:45 pm
by invisibled
Ok so this is really frusterating me. I have checked tons of examples around the net and tried every way and NONE of them are working. I'm not new to PHP, and i really dont know why this is happening.

All i want to do is take an array of checkbox's and insert them into different columns in the database. Here is my HTML

Code: Select all

 
<label for="title">Title:</label>
<input type="text" name="title" id="title" value="<?php print $title;?>" />  
 
<label for="thumbnail">Thumbnail:</label>
<input type="file" name="thumbnail" id="thumbnail" />
 
<label for="description">Description:</label>
<textarea name="description" id="description"> <?php print $description;?> </textarea> 
 
<div class="responsible">
    <label for="responsible" id="responsible">Responsible For:</label>
 
    <input type="checkbox" name="resp" value="arch" id="arch"  />
    <label for="arch">Architecture</label>
    <br />
    <input type="checkbox" name="resp" value="bran" id="bran" />
    <label for="bran">Branding</label>
    <br />
    <input type="checkbox" name="resp" value="desi" id="desi" />
    <label for="desi">Design</label>
    <br />
    <input type="checkbox" name="resp" value="idev" id="idev" />
    <label for="idev">Interface Development</label>
    <br />
    <input type="checkbox" name="resp" value="bkpg" id="bkpg" />
    <label for="bkpg">Backend Programming</label>
    <br />
    <input type="checkbox" name="resp" value="ddev" id="ddev" />
    <label for="ddev">Database Development</label>
</div>
and here is my php

Code: Select all

 
if(isset($_POST['create'])):
    extract($_POST);
    
    mysql_query("INSERT INTO $table VALUES (
        '', 
        '$title', 
        '$thumbnail',
        '$description',
        '$arch',
        '$desi',
        '$bkpg',
        '$bran',
        '$idev',
        '$ddev'
    )") or die(mysql_error());
endif;
 
currently there is nothing there to handle the checkbox array, so what would i have to put in the php to handle it?

thanks!

Re: inserting checkbox data into database

Posted: Mon Jul 13, 2009 7:08 pm
by Architek
in some of my scripts... I am a newbie...

I format like this....

either run an isset for each

Code: Select all

 
if(isset($_REQUEST["email"])) $email=$_REQUEST["email"];
if(isset($_REQUEST["phone"])) $phone=$_REQUEST["phone"];
 
or just pull any value that may or not be set...

Code: Select all

 
$sql="INSERT INTO $tbl_name(name, email, comment, datetime)VALUES('$_POST[name]', '$_POST[email]', '$_POST[comment]', '$datetime')";
 
I may do it totally wrong but it works for me at this point while I learn.

Re: inserting checkbox data into database

Posted: Mon Jul 13, 2009 8:55 pm
by Skara

Code: Select all

   <input type="checkbox" name="resp" value="arch" id="arch"  />
    <input type="checkbox" name="resp" value="bran" id="bran" />
    <input type="checkbox" name="resp" value="desi" id="desi" />
Each checkbox must have a different name. Radio buttons all have the same name as only one can be checked.
If a user checks more than one of these boxes, only the last value will be sent.

Code: Select all

   <input type="checkbox" name="arch" value="arch" />
    <input type="checkbox" name="bran" value="bran" />
    <input type="checkbox" name="desi" value="desi" />

Code: Select all

$arch = isset($_POST['arch']);
$bran = isset($_POST['bran']);
//...
If there is actual content within a checkbox, e.g.

Code: Select all

<input type="checkbox" name="foo" value="bar" />
you can retrieve "bar" the same as any other variable:

Code: Select all

$foo = isset($_POST['foo']) ? $_POST['foo'] : '';
But since the true value of a checkbox is binary--either checked or not--this is useless. Just FYI.

Re: inserting checkbox data into database

Posted: Tue Jul 14, 2009 4:51 pm
by invisibled
ahhhhhhhhhh, i get it now. I didnt realize it was binary. Thanks!