By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,667 Members | 2,261 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,667 IT Pros & Developers. It's quick & easy.

INSERT INTO statement won't take variables as values!

P: 3
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
Expand|Select|Wrap|Line Numbers
  1. <?php 
  2.  
  3. session_start(); 
  4.  
  5. $host="localhost"; // Host name 
  6. $username="root"; // Mysql username 
  7. $password="xxxxxxx"; // Mysql password 
  8. $db_name="test"; // Database name 
  9. $tbl_name="Student"; // Table name 
  10.  
  11. // Connect to server and select databse.
  12. mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
  13. mysql_select_db("$db_name")or die("cannot select DB");
  14.  
  15. // username and password sent from signup form 
  16. $quizID=$_POST['quizID']; 
  17. $score=$_POST['Score'];
  18.  
  19. $sql="INSERT INTO Marks (field1, filed2) VALUES ('10', '1')";
  20. mysql_query($sql);
  21.  
  22. echo $score;
  23. echo $quizID;
  24.  
  25. session_register("quizID");
  26. session_register("Score"); 
  27.  
  28. //echo $score;
  29. //echo $quizID;
  30. //header("location:welcome.xml");
  31. ?> 
  32.  
  33. 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.
  34.  
  35. 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.
  36.  
  37. This is the javascript function that is called on clicking on the hyperlink:
  38.  
  39. function setItemDetails()
  40. {
  41.  
  42. alert("test complete");
  43.  
  44. document.myform.quizID.value = 1;
  45. document.myform.Score.value = correctAnswers;
  46. document.myform.submit();
  47. }
  48.  
  49.  
  50. This is where the hyperlink is
  51.  
  52.  
  53. <body bgcolor=gray>
  54.  
  55. <form name="myform" action="updatedata.php" method="post">
  56.  
  57. <input type="hidden" name="quizID" value="1" />
  58. <input type="hidden" name="Score" value="correctAnswers" />
  59.  
  60. <a href="javascript:setItemDetails()">Finish</a>
  61. </form>
  62.  
  63.  
  64. </body>
  65. </html>
  66.  
  67. The variables are declared globally 
  68.  
  69.  
  70. // Define global variables and arrays
  71. var questionIndex    = 0;
  72. var checker         = true;
  73. var keeper         = new Array();
  74. var correctAnswers     = 0;
  75. var quizID              = 1;
  76.  
Any idea why this isnt working?
Feb 28 '08 #1
Share this Question
Share on Google+
2 Replies


ronverdonk
Expert 2.5K+
P: 4,258
Showing the code, the way you did, does not tempt many people to go through it.

First read the Posting Guidelines and enclose any code shown within the appropriate code tags.

moderator
Feb 28 '08 #2

ronverdonk
Expert 2.5K+
P: 4,258
I sure hope that field 'filed2' is a typo, otherwise you have the error.

It would be a lot better when MySQL instructions were tested for the completion code. That is the only way of catching any spelling or syntax errors. So change your statement like this :[php]mysql_query($sql)
or die("INSERT error: ".mysql_error());[/php]and the error (when in the insert) will be shown.

Ronald
Feb 28 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.