Save checkbox data in the database (1) Introduction Checkbox is a very useful page form, which can even allow users to choose all items or one without choosing multiple options. However, although this is a very excellent form element, in our work, there is always some confusing situation in how to properly save the selection. This article will describe how to properly save the Checkbox selection in the database under the way to follow the good database design principles. This article will explain how to properly save the selection in the user database. Although it includes useful PHP code, I will express them from the point of view of the database, so you can easily use any database and server-side scripting language. I just want to provide a way to do, so that you can apply it in your own site. If you want to run the source code here, you need to install PHP, MySQL, and web servers. Example 1: Recruitment site If you are asked to be a recruitment website, the software developers who allow job hunting fill in their skills, allowing employers to access this site and find suitable employees based on job seekers. You also know that a developer's skill will be more than one, so you decide to design your site. Each job seeker will allow access to this site, register a user, and enter his skills, checkbox will send it, you may want to ask for this page: __ php __ mysql __ zope __ perl __ javascript __ jsp [Submit Each job search can choose the skills he owns. Obviously this option is different for different people. A person may be PHP and MySQL, and others may just just JSP. How will you save these options? A natural idea is to build a field for each option, which is working properly. But then you may find that when you want to expand or adjust, trouble, you may have to modify your table structure. A good method should be like this: You should have a user table containing user registration information, such as user name, password, and other content you need.
If you use the source code given later in this article, you have to build a simple table as follows: ID username 1 User1 2 USER2 3 USER3 We first build a table "const_skills" with the following SQL statement: SQL> Create Table const_skills (ID INT NOT NULL Primary Key, Value Varchar (20)); Now we join 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 Value) (5, "javascript"); SQL> INSERT INTO const_skills (id, value) VALUES (6, "jsp"); your const_skills should now be like this: ID value 1 PHP 2 MySQL 3 Zope 4 Perl 5 Javascript 6 JSP this table only allows the user to select the appropriate skills, now, to build a table lookup_skills use the following SQL: SQL> CREATE tABLE lookup_skills (id int not null auto_increment primary key, uid int, skill_id int); this table lookup_skills The purpose is to provide a mapping relationship between the user form to the development skill table. In other words, it allows us to save developers and their skills, for example, when the job seeker completes the choice of click Submit, we will fill in the values selected in Checkbox. For each selected skill, we add a record, write down the ID and the ID of the user ID and the option. (I want everyone to know. I translated this, 嘿嘿 ...) Before we see this inserted code, let's design this page, there is a form, we can query the database and take the checkbox tab From the const_skills table, build this Checkbox form.
Code is as follows: