checkboxes mysql php

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
designingamy
Forum Newbie
Posts: 6
Joined: Mon Jun 23, 2008 3:32 pm

checkboxes mysql php

Post by designingamy »

Hello,
I am having a hard time getting checkboxes. What I would like to do is if it is checked, it go into the database. This is how I've set them up in my database:

tinyint 1, unsigned, null

This is how I've set it up in my html form:

Code: Select all

 
<form action="" method=POST>
<input type=checkbox name=features[] value=alarm> Alarm
<input type=checkbox name=features[] value=ac>Central AC
<input type=checkbox name=features[] value=cyard>Courtyard
</form>
 
What I don't understand is the php part of it. I'm a newbie when it comes to learning this stuff, so please bear with me! This is what I've gathered so far, but I'm so unsure of what it all means.

Code: Select all

 
<?php
 
foreach($features as $featuresname)
{ 
          $query = "insert into table (alarm,ac, cyard)values(????????????????????)"; 
          $result = mysql_query($query) or die ("query not made"); 
?>
 
And this is what I gathered if I were on a page where I needed to pull that info back out of the database:

Code: Select all

 
<?php
include ("");
 
mysql_connect("$host", "$user", "$password") or die ("cannot connect to server");
mysql_select_db("$database") or die ("cannot select DB");
 
 
!isset($_POST['features']));
foreach ($features as $featuresname)
{
echo "$featuresname";
}
?>
 
Any ideas or suggestions will be much appreciated!!!
Thanks,
~Amy
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: checkboxes mysql php

Post by califdon »

Checkboxes, unlike radio buttons, need separate names, since each one is a different field in your table, and each one can be either True or False.

Code: Select all

<form action="" method=POST>
<input type=checkbox name="alarm" value=False> Alarm
<input type=checkbox name="ac" value=False>Central AC
<input type=checkbox name="cyard" value=False>Courtyard
...
<input type=submit value="Submit data">
</form>

Code: Select all

$alarm=mysql_real_escape_string($_POST['alarm'];
$ac=mysql_real_escape_string($_POST['ac'];
$cy=mysql_real_escape_string($_POST['cyard'];
$query = "INSERT INTO table (alarm,ac, cyard) VALUES ($alarm,$ac,$cyard)";
$result = mysql_query($query) or die ("Insert query failed");
Hope that gets you started.
designingamy
Forum Newbie
Posts: 6
Joined: Mon Jun 23, 2008 3:32 pm

Re: checkboxes mysql php

Post by designingamy »

Oh, okay. I see how you did that. Thanks bunches!

~Amy
designingamy
Forum Newbie
Posts: 6
Joined: Mon Jun 23, 2008 3:32 pm

Re: checkboxes mysql php

Post by designingamy »

Okay, and one more thing....You said they can either be true or false. What do you mean by that? If one is checked, how does it go into the database. How is it decided if one is false or true?

~Amy
designingamy
Forum Newbie
Posts: 6
Joined: Mon Jun 23, 2008 3:32 pm

Re: checkboxes mysql php

Post by designingamy »

If I were to do it this way, would it work well with my database?

Code: Select all

 
<form action=''' method=POST>
<input type=checkbox name="alarm"<?php if($alarm == "on"){echo" CHECKED";}?>> Alarm<br>
<input type=checkbox name="ac"<?php if($ac == "on"){echo" CHECKED";}?>> Central AC<br>
<input type=checkbox name="cyard"<?php if($cyard == "on"){echo" CHECKED";}?>> Courtyard<br>
<input type="submit" name="submit" value="Submit">
</form>
 
Notice, I didn't use any values...is that okay to not do that?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: checkboxes mysql php

Post by califdon »

designingamy wrote:If I were to do it this way, would it work well with my database?

Code: Select all

 
<form action=''' method=POST>
<input type=checkbox name="alarm"<?php if($alarm == "on"){echo" CHECKED";}?>> Alarm<br>
<input type=checkbox name="ac"<?php if($ac == "on"){echo" CHECKED";}?>> Central AC<br>
<input type=checkbox name="cyard"<?php if($cyard == "on"){echo" CHECKED";}?>> Courtyard<br>
<input type="submit" name="submit" value="Submit">
</form>
 
Notice, I didn't use any values...is that okay to not do that?
My blunder! You have it correct. A good reference can be found at http://www.w3schools.com/HTMLDOM/prop_c ... hecked.asp. If I confused you, my apologies.
designingamy
Forum Newbie
Posts: 6
Joined: Mon Jun 23, 2008 3:32 pm

Re: checkboxes mysql php

Post by designingamy »

My questions still weren't answered and you steered me down the wrong path. I'm done with this website.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: checkboxes mysql php

Post by califdon »

designingamy wrote:My questions still weren't answered and you steered me down the wrong path. I'm done with this website.
Bye.
designingamy
Forum Newbie
Posts: 6
Joined: Mon Jun 23, 2008 3:32 pm

Re: checkboxes mysql php

Post by designingamy »

Goodbye. Good riddance.
Post Reply