Page 1 of 1

checkboxes mysql php

Posted: Mon Jun 23, 2008 4:04 pm
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

Re: checkboxes mysql php

Posted: Mon Jun 23, 2008 5:41 pm
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.

Re: checkboxes mysql php

Posted: Tue Jun 24, 2008 8:54 am
by designingamy
Oh, okay. I see how you did that. Thanks bunches!

~Amy

Re: checkboxes mysql php

Posted: Tue Jun 24, 2008 8:57 am
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

Re: checkboxes mysql php

Posted: Tue Jun 24, 2008 9:13 am
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?

Re: checkboxes mysql php

Posted: Tue Jun 24, 2008 11:35 am
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.

Re: checkboxes mysql php

Posted: Wed Jun 25, 2008 7:46 am
by designingamy
My questions still weren't answered and you steered me down the wrong path. I'm done with this website.

Re: checkboxes mysql php

Posted: Wed Jun 25, 2008 11:30 am
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.

Re: checkboxes mysql php

Posted: Wed Jun 25, 2008 12:46 pm
by designingamy
Goodbye. Good riddance.