inserting checkbox data into database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
invisibled
Forum Contributor
Posts: 112
Joined: Sun Apr 29, 2007 3:35 pm
Location: New Westminster

inserting checkbox data into database

Post 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!
User avatar
Architek
Forum Commoner
Posts: 44
Joined: Wed Jul 01, 2009 5:01 am
Location: Portland OR

Re: inserting checkbox data into database

Post 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.
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Re: inserting checkbox data into database

Post 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.
invisibled
Forum Contributor
Posts: 112
Joined: Sun Apr 29, 2007 3:35 pm
Location: New Westminster

Re: inserting checkbox data into database

Post by invisibled »

ahhhhhhhhhh, i get it now. I didnt realize it was binary. Thanks!
Post Reply