I made a little online survey. Students ask questions and choose answers to 14 questions. First the answers are written to a table surveyU6_3_data. That works fine.
I want to collect the totals for each question and each possible answer.
The code below works fine on my localhost, does exactly what I want, no errors in apache2 error.log
This particular part from the code below echoes the SQL to the success page for my information:
echo 'SQL is ' . $mycount . '<br>';
I can copy and paste these commands in my webpage phpmyadmin and they work fine. For example:
UPDATE surveyU6_3_totals SET A3_total = A3_total + 1 WHERE Qnr = ‘Q1’
But they are not doing what they should do when the form is submitted on my webpage.
Like I said, this works perfectly on my laptop. Any ideas why it won’t work on my webpage?? PHP version problems maybe?? I have the latest version on my laptop, the webpage is not so up to date.
// the possible answers
$str = "ABCDEFG";
$length = strlen($str);
// loop through the possible answers
for ($index = 0; $index < $length; $index++) {
//echo 'the letter is: ' . $str[$index] . '<br>';
// loop through all questions, 1 to 14
for ($i=1; $i <= 14; $i++) {
$letter = ${"q$i"};
//echo "Answer letter is " . $letter . '<br>';
$questionnr = 'Q' . $i;
if ($letter == $str[$index]){
try{
//echo "Question number is " . $questionnr . '<br>';
$columnnr = intval($index) + 1;
$column = 'A' . $columnnr . '_total';
//echo '$column is ' . $column . '<br>';
//exit();
//$mycount = 'UPDATE `surveyU6_3_totals` SET `A1_total`= A1_total+1 WHERE Qnr=' . $questionnr;
//$mycount = 'UPDATE `surveyU6_3_totals` SET `A1_total`= A1_total+1 WHERE Qnr = ' . $questionnr;
//$mycount = 'UPDATE surveyU6_3_totals SET ? = ? + 1 WHERE Qnr= ?';
$mycount = 'UPDATE surveyU6_3_totals SET ' . $column . ' = ' . $column . ' + 1 WHERE Qnr = \'' . $questionnr . '\'';
echo 'SQL is ' . $mycount . '<br>';
//exit();
$mystmt2 = $pdo->prepare($mycount);
$mystmt2->execute([$column, $column, $questionnr]);
}
catch(PDOException $e){
$_SESSION['error'] = $e->getMessage();
//echo $_SESSION['error'];
//header("Location: ../19BEwW1.html");
//include '/20BEhw/html/20BEsW21.html.php';
header('Location: /19BEcw/BE4p88survey3.html.php');
exit();
}
}
}
}