保存图片数据库:PHP中在数据库中保存Checkbox数据(2)来源: 发布时间:星期四, 2009年2月12日 浏览:148次 评论:0
在数据库中保存Checkbox数据(2) 这代码是非常简单 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 用这种思路方法来动态创建 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 插入 lookup_skills 现在我们已经创建了这个表单 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 下面 ![]() ![]() ![]() ![]() ![]() /* the function we call to insert. the $skills argument is the skills 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 ![]() ![]() } ?> 很简单吧 ![]() ![]() ![]() ![]() ![]() ![]() ![]() 搜索 当 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() /* builds a query to search for the skills checked off in the $skills function skill_search($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 .= \";\"; ![]() } } ?> 如果执行了搜索 PHP 和 Javascript ![]() ![]() 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; 这个 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 整理总结 好了 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() [第 2节完] ---------------------------- 作者:Dan LaFlamme 译者:sharetop([email protected]) 0
相关文章读者评论发表评论 |
|