简体   繁体   中英

Need help passing survey responses from HTML form to MySQL table

I'm a bit stumped here...

I've got a web page that displays a list of survey questions that are stored in a db table, and I'm trying to gather ratings responses on each question (strongly disagree -> strongly agree) and pass the values back to another table in my db.

Here's a snippet of the form code:

<form action="submitAnswers.php" name="subAns" method="post">
        <fieldset>
            <legend>Survey Questions</legend>
            <input type="hidden" name="survey_id" value="<?php echo ($survey_id); ?>">
            <table width=90% border=0 cellpadding='2' cellspacing='2'?>

            <tr bgcolor=#D0D0D0>

                <th>Question</th>
                <th>Strongly Disagree</th>
                <th>Somewhat Disagree</th>
                <th>Neither Disagree nor Agree</th>
                <th>Somewhat Agree</th>
                <th>Strongly Agree</th>
            </tr>
            <tr>    
            <?php
                while ($row  =  mysql_fetch_array($res, MYSQL_ASSOC)) 
                {
                    echo "<td bgcolor='#E8E8E8'><font size='-1'>$row[quest_order]) $row[quest_text]</font></td>";

                    for ($i = 0; $i < 5; $i++) 
                    {
                    //echo "<input type='hidden' name='qID' value='$quest_id'>";

                    echo "<td bgcolor='#E8E8E8'><input type='radio' name='$row[quest_id]' value='$i'></td>";
                    }           

                    echo "</tr>";
                }
            ?>      

            <br>            
            </table><br>
            <input type="submit" name="submitAnswers" value="Submit Survey"></fieldset>
    </form>

And here's the PHP in the 'submitAnswers.php' file:

$survey_id=$_POST['survey_id'];

    $sql = "INSERT INTO survey_responses (survey_id) 
            VALUES ('$survey_id')";

    $res = send_sql($sql, $link, $db);  


    foreach($_POST['quest_id'] as $id=>$value)
    {   

        $sql = "INSERT INTO survey_answers (response_id, quest_id, response_value) 
            VALUES (LAST_INSERT_ID(), $id, '$value')";

        $res = send_sql($sql, $link, $db) or die("Cannot add survey");

    }

My insert statement into the 'survey_responses' table works just fine, but I don't know how to correct my radio buttons used to obtain the response score for each question. The values are being passed with POST (I confirmed this by looking at print_r($_POST); , which returned, for example:

Array ( [survey_id] => 4 [6] => 0 [5] => 1 [4] => 2 [3] => 3 [2] => 4 [1] => 3 [submitAnswers] => Submit Survey )

but clearly I'm going about this wrong, as I'm not able to successfully iterate through and insert the question id# and response value for each one. I'm sure this is relatively simple, but I'm still new to programming and have been going over this for hours with little to no luck.

Just hoping someone can help me fix this, and hopefully in the process better explain, or point me to a good resource covering how to handle dynamically sized forms that are retrieving database records and passing various amounts of information back to the db.

Thanks!

Use

$sql = "INSERT INTO survey_answers (response_id, quest_id, response_value) 
        VALUES ('".LAST_INSERT_ID()."', '".$id."', '."$value."')";

instead

$sql = "INSERT INTO survey_answers (response_id, quest_id, response_value) 
        VALUES (LAST_INSERT_ID(), $id, '$value')";

Ok I think I've got this licked, though there may be a better solution. In my form, I changed the radio buttons to read as follows:

echo "<td bgcolor='#E8E8E8'><input type='radio' name='quest".$row[quest_id]."' value='$i'></td>";

The only change was adding 'quest' as a prefix to the ID# in the name attribute. In my PHP file:

foreach ($_POST as $name=>$value)
{
    if (strpos($name, "quest") !== FALSE)
    {
    $qID = substr($name, 5);
    $sql = "INSERT INTO survey_answers (response_id, quest_id, response_value) 
        VALUES (LAST_INSERT_ID(), '$qID', '$value')";

    $res = send_sql($sql, $link, $db) or die("Cannot add survey");
    }
}

I'm checking the post values for those with the 'quest' prefix, then extracting the id# via substr .

If there's a better way I'm all ears...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM