Click to See Complete Forum and Search --> : Checkbox in Mysql
prophit
12-21-2006, 08:01 PM
Creating a form for work. Wanted to know how i would go about entering in a checkbox value into mysql using php
Here is the code I have now.
<table>
<tr><td align="center">Wired</td><td align="center">Wireless</td><td align="center">Rooms</td></tr>
<tr><td align="center" bgcolor="FFFF66"><input type="text" name="wired"></td><td align="center" bgcolor="FFFF66"><input type="text" name="wireless"></td><td align="center" bgcolor="FFFF66"><input type="text" name="rooms"></td></tr>
<tr><td></td><td></td><td></td></tr>
<tr><td align="center">Maintance</td><td align="center">Terminated</td><td align="center">Non-SL</td></tr>
<tr><td align="center" bgcolor="FFFF66"><input type="checkbox" name="maint"></td><td align="center" bgcolor="FFFF66"><input type="checkbox" name="term"></td><td align="center" bgcolor="FFFF66"><input type="checkbox" name="nonsl"></td></tr>
</table>
When they submit the form how do i go about storing the checkbox values, either checked or not checked in mysql?
Tyree
12-21-2006, 09:08 PM
Give each checkbox a value and store it like you would any other input value.
value="1"...if checked, '1' will be put in the db. If not, then nothing will be put in the db. If you want to make sure SOMETHING is put in the table either way, then do a test to see if the checkbox value is '1'. If not, define it as '0'.
if ($_POST['maint'] != 1) {
$_POST['maint'] = 0;
}
prophit
12-22-2006, 01:36 PM
Ok so now how do i go about show this is my results when i query the database?
I found this but i cant seem to get it to work right
<input type="checkbox" name="maint" value="1"
<?php if (!empty($row['maint'])) echo "checked"; ?> />
Tyree
12-22-2006, 02:01 PM
So, when you get info from the db you want the checkbox to be checked if there's a value in the db for that field...gotcha.
Okay, IF you decided to go with the 1 for checked and 0 for unchecked...
What you had is very close...
<input type="checkbox" name="maint" value="1"
<?php print (($row['maint'] == "1")? 'checked="checked"' : '' ); ?> />
prophit
12-22-2006, 02:31 PM
Hey Tryee,
Here is my bit of code that im trying to add this too, Still having problems
echo '<table border="0">'.
'<tr><td align="center">Wired</td><td align="center">Wireless</td><td align="center">Rooms</td></tr>'.
'<tr><td align="center" bgcolor="FFFF66">'.$row['wired'].'</td><td align="center" bgcolor="FFFF66">'.$row['wireless'].'</td><td align="center" bgcolor="FFFF66">'.$row['rooms'].'</td></tr>'.
'<tr><td></td><td></td><td></td></tr>'.
'<tr><td align="center">Maintance</td><td align="center">Terminated</td><td align="center">Non-SL</td></tr>'.
'<tr><td align="center" bgcolor="FFFF66">'.$row['maint'].'</td><td align="center" bgcolor="FFFF66">'.$row['term'].'</td><td align="center" bgcolor="FFFF66">'.$row['nonsl'].'</td></tr>'.
'</table>'.
Thanks alot for this help
Tyree
12-22-2006, 02:45 PM
Here's what I think you're going for:
echo '<table border="0">'.
'<tr><td align="center">Wired</td><td align="center">Wireless</td><td align="center">Rooms</td></tr>' .
'<tr>' .
'<td align="center" bgcolor="FFFF66">'.$row['wired'].'</td>' .
'<td align="center" bgcolor="FFFF66">'.$row['wireless'].'</td>' .
'<td align="center" bgcolor="FFFF66">'.$row['rooms'].'</td>' .
'</tr>' .
'<tr><td></td><td></td><td></td></tr>'.
'<tr>' .
'<td align="center">Maintenance</td>' .
'<td align="center">Terminated</td><td align="center">Non-SL</td>' .
'</tr>' .
'<tr>' .
'<td align="center" bgcolor="FFFF66">' .
'<input type="checkbox" name="maint" value="1"' .
(($row['maint'] == "1")? 'checked="checked"' : '' ) .
'/>'.$row['maint'].'</td>' .
'<td align="center" bgcolor="FFFF66">' .
'<input type="checkbox" name="term" value="1"' .
(($row['term'] == "1")? 'checked="checked"' : '' ) .
'/>'.$row['term'].'</td>' .
'<td align="center" bgcolor="FFFF66">' .
'<input type="checkbox" name="nonsl" value="1"' .
(($row['nonsl'] == "1")? 'checked="checked"' : '' ) .
'/>'.$row['nonsl'].'</td>' .
'</tr>'.
'</table>';
I also added a semi-colon to the end of the string. That may not be the end...may just be what you showed me of the string. If not, make sure you end with the semi-colon.
Working version of that code:
http://www.tyreeonline.com/tests/on-off/
Oh, just noticed there's a spelling mistake...Maintenance
prophit
12-22-2006, 03:00 PM
Hey Tyree,
Thanks alot for this help. Everytime I get a project like this i learn soo much. Im gonna try to fix the last part but if i have problems I will be sure to ask.
I just want the check box to show not the checkbox and 1.
Thanks again for all your help!!!
prophit
12-22-2006, 03:04 PM
perfect got it figured out
<input type="checkbox" name="maint" ' .(($row['maint'] == "1")? 'checked="checked"' : '' ) . '/></td>'
Just took out the extra .$row at the end. Thanks alot bud!!!
Tyree
12-22-2006, 03:07 PM
I didn't think you wanted that, but I left it in so you could see for sure the checkboxes were behaving properly. Don't forget the spelling mistake...maintenance. :)
Glad to help! Good luck!