1
<?PHP
session_start();
if (isset($_SESSION['hasVoted']) && $_SESSION['hasVoted'] == '1') {
        print "You've already voted";
    }
else {
    if (isset($_POST['Submit1'])) {
        $ID = $_SESSION['user']; // obtain the ID of the user from the login session
        var_dump($ID);
        $string_results = $_POST['h2'];
        $selected_radio = explode(',', $string_results);
        var_dump($selected_radio);
        $user_name = "root";
        $password = "";
        $database = "surveyTest";
        $server = "127.0.0.1";
        $SQL = "SELECT * FROM tblquestions";
        $db_handle = mysql_connect($server, $user_name, $password);
        $db_found = mysql_select_db($database, $db_handle);
        if ($db_found) {
            $result = mysql_query($SQL);
            $numRows = mysql_num_rows($result); //return number of rows in the table
            var_dump($numRows);
            $qNum = 'q1';
            for ($i = 0; $i < $numRows; $i++)
            {
                var_dump($qNum);
                $_SESSION['hasVoted'] = '1';
                $selected_Value = $selected_radio[$i];
                var_dump($selected_Value);
            //==================================================================================
            //  SET Multiple rows IN THE answers TABLE for each question for a given student.
            //==================================================================================
                $SQL = "INSERT INTO answers (QID, Answer, SID) VALUES ('$qNum', '$selected_Value', '$ID')";
                $result = mysql_query($SQL);
                    $question_Number = ltrim($qNum,'q');
                    $question_Number++;
                    $qNum ='q'.$question_Number;
            }
                mysql_close($db_handle);
                print "Thank you for participating!";
        }
        else {
        print "database error";
        }
    }

    else {
        header("location:login.php");;
    }
}
session_destroy();
?>

<html>
<head>
<title>Process Survey</title>
</head>



<body>

</body>
</html>

When I execute the above code, the $SQL statement that consists of the INSERT only gets executed once. This means that it is only adding a single record to the database. However, I need it to add several records to the database which is equal to $numRows. The print out of $qNum and $selected_Value show the right thing. Any suggestions?

8
  • Add a call to mysql_error, and see if the database is generating an error. You're also doing a lot of work to generate $qNum - doesn't it just have the value of "q" . ($i + 1)? Commented Jul 9, 2013 at 20:19
  • 1
    You should use mysqli_, since mysql_ is depreciated Commented Jul 9, 2013 at 20:19
  • don't use multiple inserts, when you could build just one Commented Jul 9, 2013 at 20:21
  • Never trust user input! If you don't check user-supplied variables, you will get hacked after a few days (if you're lucky)... Commented Jul 9, 2013 at 20:22
  • How can I only use one statement? Commented Jul 9, 2013 at 20:23

2 Answers 2

2

try again using $result = mysql_query($SQL) or die(mysql_error());

also are the Primary keys ok on your table?

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for this, this is one thing that I missed. So, to find out the error is it just echo mysql_errno($db_handle)
mysql_error, not mysql_errno which will not give you as useful information.
no just the above code and PHP will print out any faults you might have.
0

In order for your code to work correctly you would have to have the same number in your $numRows variable, which is based on your $result variable, as you do in count($selected_radio), which is probably not the case. I'm also concerned that you may not have an HTML <input type='radio' value='whatever' name='h2' />. Why would this be a comma separated String? A radio button should only return the value which is 'checked'. Anyways, maybe you have name='h2', in a hidden input or something. In this case your loop should look something like:

for($i=0,$l=count($selected_radio); $i<$l; $i++){
  //your code in here
}

If I was you I would use a Library like PHPglue to handle your Sticky Form issues. Visit https://www.PHPglue.com for more details.

1 Comment

In my case, h2 is a hidden input type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.