万卷书   首页 > 电脑书籍 > PHP > PHP中在数据库中保存Checkbox数据(2)
 
   下面的代码,我们假定我们可能访问这个userid用这个变量名$uid,下面就是插入记录的函数代码:


/* 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);
}

?>

   很简单吧。现在你知道如何从表const_skill读记录来动态创建一个表单,也知道如何保存用户选择的技能到表lookup_skills中。下面我们要做什么?让我们看一下搜索吧。

上一页 下一页