INSERT INTO statement won't take variables as values!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • adamace5o
    New Member
    • Feb 2008
    • 3

    INSERT INTO statement won't take variables as values!

    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
    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;
    Any idea why this isnt working?
    Last edited by ronverdonk; Feb 28 '08, 07:02 PM. Reason: code within tags
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    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

    Comment

    • ronverdonk
      Recognized Expert Specialist
      • Jul 2006
      • 4259

      #3
      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($sq l)
      or die("INSERT error: ".mysql_error() );[/php]and the error (when in the insert) will be shown.

      Ronald

      Comment

      Working...