Page 1 of 1
Checkbox database question
Posted: Tue Aug 18, 2009 2:18 pm
by neocamel
I was hoping someone could point me in the right direction as far as how to accomplish this task. I want to use PHP and a mySQL database.
What I'd like is a page with a dropdown menu, once you make a selection from that dropdown menu and hit submit, the data of the table that you chose in the dropdown is displayed.
The data in the mySQL table only needs to be the value of a checkbox. Basically binary data, each cell in the table is either "on" or "off". I'd like that data to be reflected on the webpage as checkboxes. If a cell in the mySQL table is "on", then I would like the corresponding checkbox on the web page to be checked, if it is "off" then I want the checkbox unchecked.
Finally, I would like the user to be able to check or uncheck checkboxes, and click a second submit button, that would update the information they changed in the mySQL table.
Thanks in advance!
Re: Checkbox database question
Posted: Tue Aug 18, 2009 5:28 pm
by AlanG
The checkbox section is very easy to achieve. The key thing to remember is that html checkboxes, if checked submit their value, otherwise they submit null.
I would use a tinyint(1) to store a 1 or 0 with a default value of 0 (for off).
Code: Select all
$database = new MySQLi(HOST,USER,PASS,NAME);
$checked = '';
// Select the field from the table and check if it contains the value 1
$query = "Select myfield From mytable";
if($result = $database->query($query)) {
$data = $result->fetch_assoc();
if($data['myfield'] == 1)
$checked = ' checked="checked"';
}
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
My Checkbox <input type="checkbox" value="1"<?php echo $checked; ?>/>
</form>
That will select the value from the database and populate the checkbox based on it's value.

Re: Checkbox database question
Posted: Tue Aug 18, 2009 6:58 pm
by neocamel
Ok that part works beautifully! If I go into phpmyadmin and set the value of the field to "1", then when I load the php page I made, the box is checked, if I empty the field, the checkbox goes away!
Is it going to be possible to have a "submit" button, that if the user clicks it, it will submit any changes they made to the checkboxes to the database, and update the results?
Re: Checkbox database question
Posted: Tue Aug 18, 2009 7:09 pm
by neocamel
Ok I also have another question. I will have many items in the checklist, is there a more efficient way to check all the fields in a given table for their values, then return those values? Currently to check if the "name" and "email" fields have a value of 1 in the database, I have to use
Code: Select all
$checked = '';
// Select the field from the table and check if it contains the value 1
$query = "Select name From form_data";
if($result = $database->query($query)) {
$data = $result->fetch_assoc();
if($data['name'] == 1)
$checked = ' checked="checked"';
}
$checked2 = '';
// Select the field from the table and check if it contains the value 1
$query = "Select email From form_data";
if($result = $database->query($query)) {
$data = $result->fetch_assoc();
if($data['email'] == 1)
$checked2 = ' checked="checked"';
}
The checklist I'll be building will have 50 or more items that need to be checked, the code could get quite long. Or is that just the way its done?
Re: Checkbox database question
Posted: Tue Aug 18, 2009 7:14 pm
by AlanG
Code: Select all
$checked = '';
// Select the field from the table and check if it contains the value 1
$query = "Select name,email From form_data";
if($result = $database->query($query)) {
$data = $result->fetch_assoc();
if($data['name'] == 1)
$checked = ' checked="checked"';
if($data['email'] == 1)
$checked2 = ' checked="checked"';
}
I'm currently working on the update feature.
Re: Checkbox database question
Posted: Tue Aug 18, 2009 7:23 pm
by neocamel
awesome! I see how you've truncated the code! I supposed I wouldn't need to query the table results for each field entry. As I said my mySQL is still a little shaky! I'll stay tuned for the update portion!