When i try to use post variables with php and mysql i can't get the insert into statement to accept varibles as values. If i use 'test' instead of $test it does work. I suspect it is something to do with the javascript im using but i can print the correct values so why am i unable to use them to enter data to a database? sorry i am new to all this
here is the updatedata.php file
Any idea why this isnt working?
here is the updatedata.php file
Code:
<?php
session_start();
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="xxxxxxx"; // Mysql password
$db_name="test"; // Database name
$tbl_name="Student"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// username and password sent from signup form
$quizID=$_POST['quizID'];
$score=$_POST['Score'];
$sql="INSERT INTO Marks (field1, filed2) VALUES ('10', '1')";
mysql_query($sql);
echo $score;
echo $quizID;
session_register("quizID");
session_register("Score");
//echo $score;
//echo $quizID;
//header("location:welcome.xml");
?>
when i echo the values of the score and quizID varibles i get the correct data printed to screen but this data isnt entered into the database.
unlike my other insert into statment in another php file which works using a submit button in an html file, this time the php file is triggered via a hyperlink.
This is the javascript function that is called on clicking on the hyperlink:
function setItemDetails()
{
alert("test complete");
document.myform.quizID.value = 1;
document.myform.Score.value = correctAnswers;
document.myform.submit();
}
[B]This is where the hyperlink is[/B]
<body bgcolor=gray>
<form name="myform" action="updatedata.php" method="post">
<input type="hidden" name="quizID" value="1" />
<input type="hidden" name="Score" value="correctAnswers" />
<a href="javascript:setItemDetails()">Finish</a>
</form>
</body>
</html>
[B]The variables are declared globally[/B]
// Define global variables and arrays
var questionIndex = 0;
var checker = true;
var keeper = new Array();
var correctAnswers = 0;
var quizID = 1;
Comment