Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

checkbox values in db

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

checkbox values in db

Postby eye » Mon Jan 27, 2003 7:02 am

How can I save checkbox values in a db ?
...
for ($i=0; $i<count($checkbox); $i++)
mysql_query ("INSERT INTO os (checkbox) values (".$checkbox[$i].")");
...
<input type="checkbox" name="checkbox[]" value="BSD">
BSD <br>
<input type="checkbox" name="checkbox[]" value="Linux">
Linux<br>
<input type="checkbox" name="checkbox[]" value="Unix">
Unix<br>
...

thank you for any help
eye
New php-forum User
New php-forum User
 
Posts: 3
Joined: Mon Jan 27, 2003 6:56 am

Postby Joan Garnet » Tue Jan 28, 2003 4:07 pm

using a form and an INSERT
Code: Select all
$query = "insert into $tableName ($fields) values ($values)";
User avatar
Joan Garnet
Moderator
Moderator
 
Posts: 387
Joined: Sat Aug 03, 2002 2:56 am
Location: Mars

Postby eye » Wed Jan 29, 2003 1:38 am

hm, give me an example ?
eye
New php-forum User
New php-forum User
 
Posts: 3
Joined: Mon Jan 27, 2003 6:56 am

Postby Joan Garnet » Wed Jan 29, 2003 3:04 am

Code: Select all
<?php
if(isset($HTTP_POST_VARS["submit"])){
   //variables must have a value :) with your server and database information
   $conection=mysql_connect($server,$login,$pass);   mysql_select_db($database,$conection);
   $sql= "INSERT INTO ".$table." VALUES (NULL,'$check1','$check2', '$check3')";//this will produce an insert in the database. the first value (NULL) is the autoincrement ID
   mysql_query($sql);
}else{
?>

<h3>Introduce values to database</h3><p>
<form action="<?php echo $php_self; ?>" method="post">
Select fruits:<br>
apples: <input type="checkbox"  name="check1" value="apples"><br>
oranges: <input type="checkbox"  name="check2" value="oranges"><br>
lemons: <input type="checkbox"  name="check3" value="lemons"><p>
<input type="submit"  name="submit" value="Submit!">

<?php
}//end else
?>

Try it and tell us how was it
:)
User avatar
Joan Garnet
Moderator
Moderator
 
Posts: 387
Joined: Sat Aug 03, 2002 2:56 am
Location: Mars

Postby eye » Wed Jan 29, 2003 4:33 am

Works perfectly.
Thank you!!!
eye
New php-forum User
New php-forum User
 
Posts: 3
Joined: Mon Jan 27, 2003 6:56 am

Postby Redcircle » Fri Jan 31, 2003 1:08 am

I wansn't going to post but....


checkbox[] creates an array right ... so you can also use
Code: Select all
$sql= "INSERT INTO ".$table." VALUES (NULL,'$check[0]','$check[1]', '$check[2]')


that way you wouldn't have to modify yer form.

NOTE: that example will only work with register_globals turned on.
User avatar
Redcircle
Moderator
Moderator
 
Posts: 830
Joined: Tue Jan 21, 2003 10:42 pm
Location: Michigan USA

Postby Joan Garnet » Fri Jan 31, 2003 4:29 am

Yes, this is better than the first one :)
because you can separate each group of checkboxes in arrays.

Code: Select all
<?php
if(isset($HTTP_POST_VARS["submit"])){
   echo "<h2>fruits</h2>";
   echo $HTTP_POST_VARS["fruits"][0]."<br>";
   echo $HTTP_POST_VARS["fruits"][1]."<br>";
   echo $HTTP_POST_VARS["fruits"][2]."<p>";
   
   echo "<h2>animals</h2>";
   echo $HTTP_POST_VARS["animals"][0]."<br>";
   echo $HTTP_POST_VARS["animals"][1]."<br>";
   echo $HTTP_POST_VARS["animals"][2]."<p>";

   echo "<h2>array structure</h2>";
   echo "<pre>";
   print_r ($HTTP_POST_VARS);
   echo "</pre>";

}else{
?>

<h3>Introduce values to database</h3><p>
<form action="<?php echo $php_self; ?>" method="post">
Select fruits:<br>
apples: <input type="checkbox"  name="fruits[]" value="apples" checked><br>
oranges: <input type="checkbox"  name="fruits[]" value="oranges" checked><br>
lemons: <input type="checkbox"  name="fruits[]" value="lemons" checked><p>

Select animals:<br>
apples: <input type="checkbox"  name="animals[]" value="dogs" checked><br>
oranges: <input type="checkbox"  name="animals[]" value="cats" checked><br>
lemons: <input type="checkbox"  name="animals[]" value="humans" checked><p>
<input type="submit"  name="submit" value="Submit!">

<?php
}//end else
?>

will output:

fruits
apples
oranges
lemons

animals
dogs
cats
humans

array structure
Array
(
[fruits] => Array
(
[0] => apples
[1] => oranges
[2] => lemons
)

[animals] => Array
(
[0] => dogs
[1] => cats
[2] => humans
)

[submit] => Submit!
)
User avatar
Joan Garnet
Moderator
Moderator
 
Posts: 387
Joined: Sat Aug 03, 2002 2:56 am
Location: Mars


Return to PHP coding => General

Who is online

Users browsing this forum: Bing [Bot] and 5 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.