After the user has completed the quiz, their taken to a page which displays their score. The problem is i need to insert this score in to the database with the column name as a variable.
I have 6 columns in my database named quizresult1, quizresult2, all the way to 6 and some other fields including username etc.
If you look at my code below, the code works fine if i remove the variable in the sql statement and put in a column name such as quizresult1.
The problem is is that i need this column to be set depending on the quizname variable?
Thanks to anyone who can help
<?php
session_start();
$username = $_SESSION['username']; //get username
$quizname = $_SESSION['quizname']; //get quizname from quiz page
$score = $_SESSION['score']; //get score
echo $username; //testing username
echo $quizname; //testing quizname "quizresult1" <----Name of database field
echo "Congratulations on completing the quiz. Your score was ";
echo $score; //echo score
echo "<br />";
$connection = mysql_connect("localhost","root","root") or die("couldn't connect"); //connect to db
$select = mysql_select_db("login") or die ("cannot select database!"); // select db login
$sql = ("UPDATE users SET '.$quizname.' = '$score' WHERE username = '$username'"); //update the table users with the column name equal to $quizname where usernames match
mysql_query($sql);
echo "<a href='index.php'>Click</a> here to return to the the home page";
?>

New Topic/Question
Reply




MultiQuote





|