Saving Checkbox data in the database in PHP (1) _ PHP Tutorial

Source: Internet
Author: User
Saving Checkbox data in the database in PHP (1 ). The introduction of checkbox is a very useful page form item. when multiple choices are made for users, it can even allow users to select all projects or none of them. However

Checkbox is a very useful page form item. when multiple choices are made to users, it can even allow users to select all projects or none of them. However, although this is a very good form element, there is always some obfuscation in our work about how to properly save the selection items. This article will describe how to properly store the checkbox selection items in the database under the good database design principles.

Requirements

This article describes how to properly store the selected items in the user database. Although useful PHP code is included here, I will express them from the perspective of database design, so you can easily use any database or server scripting language. I just want to provide a method for you to apply it to your site. If you want to run the source code, you need to install php, mysql, and the network server.

Example 1: recruitment site

If you are asked to create a recruitment website that allows software developers who are looking for a job to enter their skills so that employers can access the site and find the right employee based on the job seeker's skills. You also know that a developer has more than one skill, so you decide to design your site in this way.

Every job seeker will be allowed to access this site, register a user, and enter his skills. Checkbox will come in handy. you may want to make a page like this:

_ PHP _ MySQL _ Zope
_ Perl _ Javascript _ JSP

[Submit]

You can select the skills you have for each job. Obviously, this option is different for different people. One person may be PHP and Mysql, and others may be JSP. How will you save these options? A natural idea is to create a field for each option so that it can work properly. However, you may find that when you want to expand or adjust the table structure, you may have to modify it.
A good method should be like this:

You should have a user table containing the user registration information, such as the user name, password, and other information you need. If you directly use the source code provided later in this article, you need to create a simple table as follows:

Id username
1 User1
2 User2
3 User3

Create a table "const_skills" and use the following SQL statement:

SQL> CREATE TABLE const_skills (
Id int not null primary key,
Value varchar (20 ));

Now we add the following skills:

SQL> INSERT INTO const_skills (id, value) VALUES (1, "PHP ");
SQL> INSERT INTO const_skills (id, value) VALUES (2, "MySQL ");
SQL> INSERT INTO const_skills (id, value) VALUES (3, "Zope ");
SQL> INSERT INTO const_skills (id, value) VALUES (4, "Perl ");
SQL> INSERT INTO const_skills (id, value) VALUES (5, "Javascript ");
SQL> INSERT INTO const_skills (id, value) VALUES (6, "JSP ");

Your const_skills should be like this:

Id value
1 PHP
2 MySQL
3 Zope
4 Perl
5 Javascript
6 JSP

This table only allows you to select the corresponding skills. now, create another table lookup_skills with the following SQL:

SQL> CREATE TABLE lookup_skills (
Id int not null auto_increment primary key,
Uid int,
Skill_id int );

This lookup_skills table aims to provide a ing between the user table and the development skill table. In other words, it allows us to save developers and their skills. for example, when a job seeker completes the selection and click submit, we will fill in the table with the values selected in the checkbox. For each selected skill, we add a record in this table to write down the user id and the selected id. (Everyone knows it. I translated it here, hey ...)

Before we look at the code for inserting a record, we should first design this page, and there should be a form in the content, we can query the database and take the checkbox label from the const_skills table, create this checkbox form item.

The code is as follows:

<? Php

/* Insert code to connect to your database here */

/* Get the checkbox labels */
$ Skills = get_checkbox_labels ("const_skills ");

/* Create the html code for a formatted set
Checkboxes */
$ Html_skills = make_checkbox_html ($ skills, 3,400, "skills []");

? >


<Html>
<Body>
<Br>
<Form name = "skills" method = "POST" action = "insertskills. php">
Check off your web development skills:
<? Echo "$ html_skills ";? >
<Br>
<Input type = "submit" value = "Submit">
</Form>
</Body>
</Html>

<? Php

Function get_checkbox_labels ($ table_name ){

/* Make an array */
$ Arr = array ();

/* Construct the query */
$ Query = "SELECT * FROM $ table_name ";

/* Execute the query */
$ Qid = mysql_query ($ query );

/* Each row in the result set will be packaged
An object and put in an array */
While ($ row = mysql_fetch_object ($ qid )){
Array_push ($ arr, $ row );
}

Return $ arr;
}

/* Prints a nicely formatted table of checkbox choices.

$ Arr is an array of objects that contain the choices
$ Num is the number of elements wide we display in the table
$ Width is the value of the width parameter to the table tag
$ Name is the name of the checkbox array
$ Checked is an array of element names that shoshould be checked
*/


Function make_checkbox_html ($ arr, $ num, $ width, $ name, $ checked ){

/* Create string to hold out html */
$ Str = "";

/* Make it */
$ Str. = "<table width =" $ width "border =" 0 "> n ";
$ Str. = "<tr> n ";

/* Determine if we will have to close add
A closing tr tag at the end of our table */
If (count ($ arr) % $ num! = 0 ){
$ ClosingTR = true;
}

$ I = 1;
If (isset ($ checked )){
/* If we passed in an array of the checkboxes we want
To be displayed as checked */
Foreach ($ arr as $ ele ){
$ Str. = "<td> <input type =" checkbox "name =" $ name "value =" $ ele-> id "";
Foreach ($ checked as $ entry ){
If ($ entry = $ ele-> value ){
$ Str. = "checked ";
Continue;
}
}
$ Str. = "> ";
$ Str. = "$ ele-> value ";

If ($ I % $ num = 0 ){
$ Str. = "</tr> n <tr> ";
} Else {
$ Str. = "</td> n ";
}
$ I ++;
}

} Else {
/* We just want to print the checkboxes. none will have checks */
Foreach ($ arr as $ ele ){
$ Str. = "<td> <input type =" checkbox "name =" $ name "value =" $ ele-> id "> ";
$ Str. = "$ ele-> value ";

If ($ I % $ num = 0 ){
$ Str. = "</tr> n <tr> ";
} Else {
$ Str. = "</td> n ";
}
$ I ++;
}

}

/* Tack on a closing tr tag if necessary */
If ($ closingTR = true ){
$ Str. = "</tr> </table> n ";
} Else {
$ Str. = "</table> n ";
}

Return $ str;
}


? >

Revoke checkbox is a very useful page form item. when multiple choices are made to users, it can even allow users to select all projects or none of them. But, do...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.