Keep Checkbox data in the database in PHP

Source: Internet
Author: User
The prefix checkbox is a very useful page form item. when multiple choices are made to users, it can even select all projects or none. However, although this is a very good form elementXianrong

Checkbox is a very useful page form item. when multiple choices are made to users, it can even select all projects or none of them. However, although this is a very good form element, there are always some easy-to-mix situations in our work regarding how to accurately retain selection items. This article describes how to keep the checkbox selection items accurately in the database under the good database design principles.

Request

This article describes how to accurately retain the selected items in the user database. Although some 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-side scripting language for implementation. I just want to provide a method for you to use on your own 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 requested to create a recruitment website, the software development staff who promise to apply for a job fill in their skills, so that the employer can visit the site and find the right employee based on the skills of the job seeker. 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 promise to visit this site, register a user, and enter his skills, so Checkbox will be useful. you may want to make such a page:

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

[Submit]

You can choose 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 do you keep these options? A natural idea is to create a field for each option so that it can work properly at the beginning. However, you may find that when you want to expand or adjust the table structure, the problem arises. you may have to modify the table structure.
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 apply 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' with the following SQL statement:

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

Tips:

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:

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.