p2p.wrox.com Forums

Need to download code?

View our list of code downloads.

  Return to Index  

php_databases thread: Storing Checkbox Values in MySQL

Message #1 by Tony Bollino <tony@a...> on Sat, 04 Jan 2003 16:00:10 -0500
Greetings all.
Thank you to all those on the list who have been helpful in the past. 
 I'm hoping you will be able to come through for me again.  Here is my 

I am usung phplist (www.phplist.com) to create a survey for teachers at 
a school.  The application only allows for radio buttons in the surveys. 
 The school wants some questions to have checkboxes so responders can 
select multiple answers for the question.  I rewrote the code to allow 
the admin to select if the answers to a survey question will be radio 
buttons or checkboxes.  That displays properly.  When I test it with 
multiple check boxes selected in one of the questions only the value of 
the last checkbox selected is entered into the database.

Here is the code:

Explaination of VARS for this example
$survey_id = The id of the survey

DB Field Definitions
surveya_type = Radio or Checkbox
surveyq_id = The question id in the database
surveya_id = The answer id in the database
surveya_name = The answer itself

This is the code to create the questions:
function list_surveyq($survey_id) {
        echo "<b><i><a 
results without taking the survey</a></i></b><P>\n";

        $strSQL = "SELECT * From bug_public_survey WHERE 
        $query1 = mysql_query($strSQL,$GLOBALS["dbconn"]);
        $survey = mysql_fetch_array($query1);
        echo "<form method=\"post\" action=\"".$GLOBALS["PHP_SELF"]."\">\n";
        echo "<input type=\"hidden\" name=\"votes\" value=\"1\">\n";
        echo "<input type=\"hidden\" name=\"survey_id\" 
//Get all the questions
        $strSQL = "SELECT * From bug_public_surveyq WHERE 
surveyq_surveyid=".$survey["survey_id"]." ORDER By surveyq_id";
        $query2 = mysql_query($strSQL,$GLOBALS["dbconn"]);
        while($surveyq = mysql_fetch_array($query2)) {
//Get all the answers
                echo "<b>".$surveyq["surveyq_name"]."</b><br>\n";
                $strSQL = "SELECT * From bug_public_surveya WHERE 
                $query3 = mysql_query($strSQL,$GLOBALS["dbconn"]);
                echo "<ul>\n";
                while($surveya = mysql_fetch_array($query3)) {

                        //THIS IS WHERE THE ANSWERS ARE PRINTED
                        //print radio button or checkbox and answer 
title for each question
echo "<input type=\"".$surveya["surveya_type"]."\" 
value=\"".$surveya["surveya_id"]."\"> ".$surveya["surveya_name"]."<br>\n";
//original code -> echo "<input type=\"radio\" 
value=\"".$surveya["surveya_id"]."\"> ".$surveya["surveya_name"]."<br>\n";
                echo "<hr></ul>\n";
        echo "<input type=\"submit\" value=\"Submit\">\n";
        echo "</form>\n";


When the form is submitted, this is the SQL that is executed.   The 
value of $votes is set to 1.  The page will reload and check to see if 
$votes is set.


if (isset($votes)) {
        $strSQL = "SELECT * From bug_public_surveyq WHERE 
        $query = mysql_query($strSQL,$GLOBALS["dbconn"]);
        while($surveyq = mysql_fetch_array($query)) {
                $strSQL = "INSERT INTO bug_public_surveyr 
(surveyr_surveyid, surveyr_surveyaid, surveyr_surveyqid) VALUES 
(".$survey_id.", ".$answer[$surveyq["surveyq_id"]].", 
                echo $strSQL;

The name of each checkbox is the same but the values are different.  Do 
both the name and values have to be different?  When it is inserted into 
the DB it shouldn't matter since the record would not be a duplicate.

Any offers of help are greatly appreciated.

Tony Bollino
Sales and Field Operations

* "Not just a solution ... an AdytumSolution." *

  Return to Index