Home > Article > Backend Development > Saving Checkbox data in the database in PHP (2)_PHP tutorial
This code is very simple, you can read it quickly. The main work is done by two functions: "get_checkbox_labels" and "make_checkbox_html". Among them, "get_checkbox_labels" queries the const_skills table and returns an array of objects. Each object has an id value and the corresponding skill name. We pass this array and some other parameters to "make_checkbox_html", this function will return a string used to generate the html code of the checkbox. Now we insert this string into the html file to generate the form we need containing various skill selections. Note that I did not pass the variable $checked to "make_checkbox_html", this parameter is an array of checked objects that we want to display. If a user learns a new skill, we can provide an "Edit Skills" page that displays a checkbox where the user's skill items saved should be pre-checked.
What are the advantages of using this method to dynamically create a form versus using a fixed html code to generate a skill checkbox? Well, maybe we allow job seekers to select an item that is not originally in our table const_skills, such as DHTML. In this way, we can insert it into the table const_skills. Then, when job seekers visit our site, they will find that there is one more DHTML options. All this without adjusting the html file.
Insert lookup_skills
Now that we have created this form, we need to save the skills selected by this user. In the make_checkbox_html function, we call each option element with skill[], which means that we can access each option as an array element. In this way we can insert this selection into the table lookup_skill. If the user selects 5 options, we insert 5 records in lookup_skill. Remember that each record in the lookup_skills table has only two fields: user id and skill id. In my example site, users can register and then create/edit their profile. You may want to use session to save the userid when they log in. But how to manage userids is beyond the scope of this article.
In the following code, we assume that we may access this userid using the variable name $uid. The following is the function code for inserting records:
/* the function we call to insert.
the $skills argument is the skills array that
is sent to the script when the user hits the submit button
*/
function insert_skills($uid, $skills) {
/* first, we'll delete any entries this user already has
in the table */
purge_lookup("lookup_skills", $uid);
/* now create the sql insert query */
$query = create_checkbox_query($skills, "lookup_skills", $uid);
/* execute the query */
mysql_query($query);
}
/* helper function for insert_skills().
removes all rows in $table with $uid */
function purge_lookup($table, $uid) {
$q = "DELETE FROM $table , WHERE uid = '$uid'";
mysql_query($q);
}
/* helper function for insert_skills().
generates the sctual SQL query */
function create_checkbox_query($arr, $table, $uid) {
$q = "INSERT INTO $table (uid, skill_id) VALUES";
foreach ($arr as $check) {
$q .= " ( $uid , $check )" . ",";
}
/* remove the last comma and return */
return substr($q, 0, -1);
}
?>
It’s very simple. Now you know how to dynamically create a form by reading records from the const_skill table, and you also know how to save the user-selected skills into the lookup_skills table. What do we do next? Let’s look at search
Search
When an employer comes looking for a web developer and he comes to your search page, you can display the same form and allow him to select the employees he wants Possessed skills. You get an array of the skills he selected, and then you can iterate through the array and use a SQL statement to find job seekers with this skill. You can display the list or results and allow the searcher to click on an item to display its details. .The following function describes how to create this query:
/* builds a query to search for the skills
checked off in the $skills array */
function skill_search($skills) {
if (!empty($skills)) {
$query = "SELECT DISTINCT user.username
FROM user, const_skills, lookup_skills
WHERE lookup_skills.uid = user. id
AND lookup_skills.skill_id = const_skills.id ";
$query .= " AND (";
foreach ($skills as $check) {
$query .= " const_skills .id = $check OR";
}
/* remove the final OR */
$query = substr($query, 0, -2);
$query .= ")";
$count = count($skills);
$query .= " GROUP BY user.username HAVING count(user.username) >= $count";
$query .= ";";
return $query;
}
}
?>
If search PHP and Javascript are executed, this function Returns this statement:
SELECT DISTINCT user.username FROM user, const_skills, lookup_skills WHERE lookup_skills.uid = user.id AND lookup_skills.skill_id = const_skills.id AND ( const_skills.id = 3 OR const_skills.id = 5 ) GROUP BY user.username HAVING count(user.username) >= 2;
This function will return the logical AND of the items you selected, that is, if we selected both PHP and Javascript , will only return usernames of candidates who have *both* PHP and Javascript skills. If you want to find candidates with any of these skills, you can use PHP *OR* Javascript. If you want to display the same records, you can remove the last "GROUP BY..." clause.
Summary
Okay, that’s it. Checkboxes are an excellent form element, as discussed in this article. I hope this helps you work with them to create a data-driven website.