Jump to content

Difficulty inserting checkbox array values into DB


TecTao

Recommended Posts

I am stumped on why an array of checkboxe values won't insert into multiple rows of the DB table.

 

After hours of searching forums and php code sites and testing different suggestions, I finally found one that creates the check boxes dynamically from a category DB table, but when the form is submitted, doesn't insert the checkbox values, or user ID variable into rows.

 

Here is the code for creating the check boxes.  Calls the values from the DB table and it works just fine.

 

The URL to view source is http://sourceandquote.com/_fcps/checkbox1a.php

<?php
include("include/session.php");
  /* insert code to connect to your database here */  
  
  /* get the checkbox labels */
  $skills = get_checkbox_labels("_categories_dsfp");
  
  /* create the html code for a formatted set of
     checkboxes */
  $html_skills = make_checkbox_html($skills, 3, 400, "skills[]");

?>


<!-- Begin checkbox html application -->

<form name="skills" method="POST" action="insertskills.php">

<?php echo "$html_skills"; ?>
   <br>
<input type="submit" value="Submit">
</form>


<?php

function get_checkbox_labels($table_name) {

  /* make an array */
  $arr = array();
  
  /* construct the query */
  $query = "SELECT * FROM $table_name";
  
  /* execute the query */
  $qid = mysql_query($query);

  /* each row in the result set will be packaged as
     an object and put in an array */
  while($row= mysql_fetch_object($qid)) {
    array_push($arr, $row);
  }
  
  return $arr;
}

/* Prints a nicely formatted table of checkbox choices.
   
   $arr is an array of objects that contain the choices
   $num is the number of elements wide we display in the table
   $width is the value of the width parameter to the table tag
   $name is the name of the checkbox array
   $checked is an array of element names that should be checked
*/   


function make_checkbox_html($arr, $num, $width, $name, $checked) {
  
  /* create string to hold out html */
  $str = "";
  
  /* make it */
  $str .= "<table width=\"$width\" border=\"0\">\n";
  $str .= "<tr>\n";

  /* determine if we will have to close add
     a closing tr tag at the end of our table */
  if (count($arr) % $num != 0) {
    $closingTR = true;
  }
  
  $i = 1;
  if (isset($checked)) {
    /* if we passed in an array of the checkboxes we want
       to be displayed as checked */
    foreach ($arr as $ele) {
      $str .= "<td><input type=\"checkbox\" name=\"$name\" value=\"$ele->id\"";
      foreach ($checked as $entry) {
    if ($entry == $ele->value) {
      $str .= "checked";
          continue;
        }
      }
      $str .= ">";
      $str .= "$ele->value";

      if ($i % $num == 0) {
        $str .= "</tr>\n<tr>";
      } else {
        $str .= "</td>\n";
      }
      $i++;
    }
  
  } else {
    /* we just want to print the checkboxes. none will have checks */
    foreach ($arr as $ele) {
      $str .= "<td><input type=\"checkbox\" name=\"$name\" value=\"$ele->id\">";
      $str .= "$ele->value";
      
      if ($i % $num == 0) {
        $str .= "</tr>\n<tr>";
      } else {
        $str .= "</td>\n";
      }
      $i++;
    }
  
  }

  /* tack on a closing tr tag if necessary */
  if ($closingTR == true) {
    $str .= "</tr></table>\n";
  } else {
    $str .= "</table>\n";
  }

  return $str;
}


?>

 

When submitted, here is the code on the submit page for inserting into DB.  Doesn't work.

 

<?php
include("include/session.php");
$uid = "1234";


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

?>

 

Although the submit page contains a function to search and delete any entries from the user, this is not necessary.  I am just having trouble trying to get the array from the checked boxes to pass and insert into the DB.

 

Thanks in advance for any assistance.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.