Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
PHP & MySQL - inserting checkbox value into database
Old 07-25-2006, 01:51 PM PHP & MySQL - inserting checkbox value into database
Average Talker

Posts: 18
Trades: 0
hello, me again.

i have 31 checkboxes in my registry form. how do i insert the value into the database?

thanks
agcaparroso is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 07-25-2006, 04:59 PM Re: PHP & MySQL - inserting checkbox value into database
Webmaster Talker

Posts: 626
Trades: 0
Here is what you need to do:

1. When setting up the column in your db you put the type as bool. 1 represents true and 2 represents false.

2. When you read the data from the page and want to save it into the db you have to convert the value to a 1 or zero. For example:

PHP Code:
<?php
   extract
($_POST);

   if(
$chkVar == "on") { $chkVar 1; } else { $chkVar 0; }
?>
That will save it in the db for you. When you want to read the db you have to set the value in document.getElementById().checked like so:

PHP Code:
// after you have read the information from the db you need to echo the checkbox to the page either checked or not

<?php
   
// read the db

   // coverting the 1 or zero to checked or not
   
if($chkVar == 1) {$chkVar "checked"; } else {$chkVar ""; } 

  
// now you need to setup the form...
  
echo "<form blah blah>";
   echo 
"<input type=\"checkbox\" name=\"chkVar\" $chkVar>";
?>
I think you get the idea... If not let me know.

Last edited by jim.thornton; 07-25-2006 at 05:01 PM..
jim.thornton is offline
Reply With Quote
View Public Profile
 
Old 07-26-2006, 01:22 PM Re: PHP & MySQL - inserting checkbox value into database
Average Talker

Posts: 18
Trades: 0
thanks. found the solution using arrays and found it easyer.
agcaparroso is offline
Reply With Quote
View Public Profile
 
Old 07-26-2006, 04:54 PM Re: PHP & MySQL - inserting checkbox value into database
AliKat's Avatar
Extreme Talker

Latest Blog Post:
Save the Children
Posts: 176
Location: MS
Trades: 0
check boxes are a pain if you dont know what you are doing. For you scripts to get all but one of the values in your check box you need to use the following naming convention

HTML Code:
<input type="checkbox" value="Data1" name="box[]"/>
Then in your PHP you'll be able to use a foreach loop

PHP Code:
foreach($_POST[box] as $b)
{
 
// Do whatever here

I know you've already figured this out but figure I'd post it anyway.
AliKat is offline
Reply With Quote
View Public Profile Visit AliKat's homepage!
 
Old 07-27-2006, 01:51 AM Re: PHP & MySQL - inserting checkbox value into database
Webmaster Talker

Posts: 626
Trades: 0
Alikat,

I never thought of doing that way. Can you explain what is being passed through the $_POST['box'] variable.

You named the checkbox box[] in HTML will that create an array? Or when you read it in PHP it will?

Would love to see more on this (ie. how you are manipulating the data in the loop).
jim.thornton is offline
Reply With Quote
View Public Profile
 
Old 07-27-2006, 09:33 AM Re: PHP & MySQL - inserting checkbox value into database
AliKat's Avatar
Extreme Talker

Latest Blog Post:
Save the Children
Posts: 176
Location: MS
Trades: 0
by putting the [] at the end of the name you are creating an array. At least PHP will interpret it that way. Inside the foreach loop if you were just going to store the stuff in a database you'd do the database queries. Not sure what you mean for the manipulation cause you could do anything then. You could sort the array, add the contents of the array up if they were that way, or even concantinate the values so that it was one string to store in the DB.

If you don't put [] you will only get the last value checked. It will overwrite everything else in the PHP value's stored.

An example
AliKat is offline
Reply With Quote
View Public Profile Visit AliKat's homepage!
 
Old 07-27-2006, 11:33 AM Re: PHP & MySQL - inserting checkbox value into database
Webmaster Talker

Posts: 626
Trades: 0
Here is what I meant:

Let's say I have a page with three checkboxes.


PHP Code:
<input type="checkbox" value="Option 1" name="box[]">
<
input type="checkbox" value="Option 2" name="box[]">
<
input type="checkbox" value="Option 3" name="box[]"
and now I am going to use the loop as suggested:

PHP Code:
foreach($_POST[box] as $b

 
// Do whatever here 

How do I access the values for boxes 1,2,3.

Would I just do something like this:

PHP Code:
foreach($_POST[box] as $b

 
// Do whatever here 
   
echo $b[1]."<br>";
   echo 
$b[2]."<br>";
   echo 
$b[3]."<br>";

jim.thornton is offline
Reply With Quote
View Public Profile
 
Old 07-27-2006, 12:00 PM Re: PHP & MySQL - inserting checkbox value into database
AliKat's Avatar
Extreme Talker

Latest Blog Post:
Save the Children
Posts: 176
Location: MS
Trades: 0
You've never used a foreach loop? (Not surprised that you've not used it but wasn't expecting a foreach question in the checkbox question)

If all you wanted to was echo out all of the contents of the check boxes your foreach loop would look like:

PHP Code:
foreach ($_POST[box] as $b) {
 echo 
$b;

A foreach loop does just that for each element in the array do what's inside.

It's the same as:

PHP Code:
$count count($_POST[box]);
for(
$i 0$i $count$i++)
{
echo 
$_POST[box[$i]];

except you don't know how many elements there are.

Last edited by AliKat; 07-27-2006 at 12:03 PM..
AliKat is offline
Reply With Quote
View Public Profile Visit AliKat's homepage!
 
Old 07-27-2006, 01:11 PM Re: PHP & MySQL - inserting checkbox value into database
Webmaster Talker

Posts: 626
Trades: 0
I have used foreach loops before, just not very many times so I always have to look up references each time.

I was mainly concern with how to access the the information.
jim.thornton is offline
Reply With Quote
View Public Profile
 
Old 07-27-2006, 03:24 PM Re: PHP & MySQL - inserting checkbox value into database
AliKat's Avatar
Extreme Talker

Latest Blog Post:
Save the Children
Posts: 176
Location: MS
Trades: 0
depends on how you do it. It's an array there are several ways to get at the information.
AliKat is offline
Reply With Quote
View Public Profile Visit AliKat's homepage!
 
Reply     « Reply to PHP & MySQL - inserting checkbox value into database
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.69581 seconds with 12 queries