All pastes #463571 Raw Edit

Something

public text v1 · immutable
#463571 ·published 2007-04-29 00:39 UTC
rendered paste body
<?php

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

?>