SQL PHP HTML ASP JavaScript articles and free scripts to download
 
 

Linking Checkbox value to a table record in PHP

Check box can be linked to records in a database table. This way we can keep the checkbox status checked or not based on the data we receive from the table. We can also read the checkbox status and update a record in a table. Let us understand this with an example.

Out of the seven days in a week we will display all the days and a checkbox for each day. User has to submit the choice of the days to the system. Next time when they visit then the same checked day status will be displayed and user can check or uncheck any days.

In a hotel you can display a menu to the guest and ask them to select their favorite dishes. We can save the user choice and display the selected items to the same guest in his next visit. Guest can alter the choice and update the record.

Here we will try with the code for selecting weekdays from a days list of checkboxes. We will select the days and on update the record will be stored in a table. While displaying the weekdays we will display by taking the values from a table.

Here is the demo ( you may not get the exact result if others are also using this page at same time )


Warning: mysql_query() [function.mysql-query]: Access denied for user 'smo123'@'localhost' (using password: NO) in /home/smo123/public_html/php_tutorial/checkbox-value.php on line 60

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/smo123/public_html/php_tutorial/checkbox-value.php on line 60

Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /home/smo123/public_html/php_tutorial/checkbox-value.php on line 60

Warning: mysql_query() [function.mysql-query]: Access denied for user 'smo123'@'localhost' (using password: NO) in /home/smo123/public_html/php_tutorial/checkbox-value.php on line 62

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/smo123/public_html/php_tutorial/checkbox-value.php on line 62

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/smo123/public_html/php_tutorial/checkbox-value.php on line 67


Here we will store values of the days ( in 1 to 7 ) by separating them by comma (,) in a table. We could have kept the weekdays in an array for simplicity but in a real project it is most likely that data will be stored in a table. So let us use one table to store weekdays and its number. Here in total we have used two tables, one stores the weekdays and its numbers (plus_weekdays) and the other table stores the userid and its selection of weekdays (plus_week). Here also we have used one record for all our tutorial and that is for userid smo. Now let us first collect the selection of userid smo stored in our table.

$query="select days from plus_week where userid='smo' ";
$row=mysql_fetch_object(mysql_query($query));


Inside our days field we store the selection with day number separated by comma. For example we have kept Monday, Wednesday, Saturday as selection of userid smo then the field days will store value 1,3,6 . So let us try to collect this value and create an array.

$days_array=split(",",$row->days);


You can understand that we have used split command to create an array which stores the day selection of userid smo. Now we will collect all the weekdays and its numbers from the table plus_weekdays and display then as checkboxes. Here before displaying we will do a small check to know whether this weekday number is there inside our $day_array or not by using in_array function. This is to know whether the weekday is previously selected by user or not. If selected previously then we will keep the checkbox checked by adding attribute checked inside the checkbox tag. For this we will use one variable $st which we will assign empty string or checked value by checking the presence inside array using in_array function. Here is the complete code.

$qt=mysql_query("select * from plus_weekdays ");
echo "<form method=post action=''><input type=hidden name=todo value=submit_form>";
echo "<table border='0' width='50%' cellspacing='0' cellpadding='0' align=center>";
$st="";
while($noticia=mysql_fetch_array($qt)){

if(@$bgcolor=='#f1f1f1'){$bgcolor='#ffffff';}
else{$bgcolor='#f1f1f1';}
if(in_array($noticia['day_no'],$days_array)){$st="checked";}
else{$st="";}
echo "<tr bgcolor='$bgcolor'>
<td class='data'><input type=checkbox name=days_array[] value='$noticia[day_no]' $st> $noticia[days]</td></tr>";

}
echo "</table>
<input type=submit value=update></form>
</center>";
As we have used two mysql tables. The sql dump of the tables along with the config file and readme file is kept inside the zip file for you to download..

Discuss this tutorial at forum


Further readings
All form components validation with post back and data locking
Checkbox
How to retain form data if validation fails?
How to retain data of a text box after submitting
Retaining status of a check box
Updating checkbox value from & to a table record.
Retaining selected data of a period button of a form
Retaining selected data of a drop down list box of a form
Validating Date: Checking if date exist
Email address validation in a form
PHP Form Validation
is_numeric to check numeric numbers
ctype_alnum to check alphanumeric characters data
ctype_alpha to check alphabetic characters data
How to take care of form & query string variables if Register global is OFF?














 

Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.

Scripts
PHP
JavaScript
PHP Tutorial Index
Popular Tutorials
Drop down list
File Upload
Signup script
Member Login
Line Graph
PHP MySQL Paging
PHP Calendar
PHP Tutorials
Date & Time
Array
String Functions
Math Functions
Form Handling
File Handling
Comment Posting
Content Management
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.