SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    cheshire
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Can anyone explain what happens to userid from a html from it disappears?

    Can anyone explain the odd behaviour of the script below?
    The first time the script below loads it performs correctly. It displays the only two rows in the table testscores that have a '2' in the 'id' column(its sent a '2' from another script which takes that info and other details eg users name and password). Then on clicking add a test score it loads the form and still displays 'Greetings Angie Graham! at the top of the page so I know it has recieved $Username as expected and therefore also $logondetails which contains $userid. However on filling out the form and pressing submit the form now displays 'Greetings !' and all rows from the table with a zero in column 'id' are displayed indicating that my $UserName and $userid have got lost somewhere!!!!The table correctly receives the test name and test score - if only I could get the id column to receive the user id instead of a zero I would be a very happy man.
    (The userid must take a default of zero when its not assigned a value)

    <html>
    <head>
    <title> The student page</title>
    </head>
    <body>
    PHP Code:
    <?php
    $Host 
    "*****";// Set the variables for the database access:
    $User "****";
    $Password "******";       
    $DBName "****";     
    $TableName "testscores";

    $link = @mysql_connect("$Host""$User""$Password");
    if (!
    $link)
     {
     print(
    "<P>Unable to connect to the " .       
     
    "database server at this time.</P>\n");      //note how 2 strings have been concatonated
     
    exit();
    }
    else
    {
    print(
    "Your now connected to the Mysql server at the host ISP.<P>\n");
    }

    if (! @
    mysql_select_db("$DBName") )
     {
     print(
    "<P>Unable to locate the $DBName database at this time.</P>\n");
     exit();
     }
     else
    {
    print(
    "The database $DBName has been located.</P>\n");
    }
    $Array=explode(" ",$logondetails);
    $userid="$Array[0]";
    $UserName=urldecode($UserName);
    print(
    "Greetings, $UserName!\n");


    // If the user wants to add a score
     
    if (isset($addscore)){

    print (
    "<FORM ACTION=\"$PHP_SELF\" METHOD=POST>\n");

    print (
    "Please enter your userid:<br>\n");
    print (
    "<INPUT TYPE=TEXT Name=\"userid\" SIZE=4><br>\n");
    print (
    "<P>Select the name of the test you have just taken :<br>\n"); 

    // Create the test pull-down menu.
    print ("Select a test:<BR>\n");
    print (
    "<SELECT NAME=testname><OPTION>Choose One</OPTION>\n");
    print (
    "<OPTION VALUE=\"Life Test\">Life Test</OPTION>\n");
    print (
    "<OPTION VALUE=\"Cells Test\">Cells Test</OPTION>\n");
    print (
    "<OPTION VALUE=\"Digestion Test\">Digestion Test</OPTION>\n");
    print (
    "<OPTION VALUE=\"Circulation Test\">Circulation Test</OPTION>\n");
    print (
    "<OPTION VALUE=\"Respiration Test\">Respiration Test</OPTION>\n");
    print (
    "<OPTION VALUE=\"Microbe Test\">Microbe Test</OPTION>\n");
    print (
    "</SELECT>\n");

    // Create the score pull-down menu.
    print ("<P>Select your score:<BR>\n");
    print (
    "<SELECT NAME=testscore><OPTION>Choose One</OPTION>\n");
    $score 1;
    while (
    $score <= 25) {
         print (
    "<OPTION VALUE=$score>$score</OPTION>\n");
         
    $score++;
    }
    print (
    "</SELECT>\n");
    print (
    "<p><INPUT TYPE=SUBMIT NAME=addscore VALUE=\"SUBMIT\">");
    print (
    "</FORM>\n");

    }
    //close the if isset



    ?>


    <P> Here are all your scores in our database: </P>
    <BLOCKQUOTE>


    <?php

    // If a score has been submitted,
     // add it to the database.
     
    if ("SUBMIT" == $addscore) {
     
    $sql "INSERT INTO $TableName SET " .
     
    "testname='$testname', " .
     
    "testscore='$testscore'," .
     
    "id='$userid'";
     if (
    mysql_query($sql)) {
     echo(
    "<P>Your score has been added.</P>");
     } else {
     echo(
    "<P>Error adding submitted score: " .
     
    mysql_error() . "</P>");
     }
     }

     
     
    // Request the scores for id=$userid from the testscores table
     
    $Query "SELECT testname, testscore FROM $TableName WHERE id='$userid'";
     
    $result mysql_query($Query); 
     if (!
    $result) {
     echo(
    "<P>Error performing query: " .
     
    mysql_error() . "</P>");
     exit();
     }


     
    // Display the test name and test score
     
    while ( $row mysql_fetch_array($result) ) {
     echo(
    "On the test called " $row["testname"] . " you scored " $row["testscore"] . "</br>");
     }


    ?>


    </BLOCKQUOTE>

    <?php
    // When clicked, this link will load this page
     // with the score submission form displayed.
    $UserName=urlencode($UserName);
    $logondetails=urlencode($logondetails);
     echo(
    "<P><A HREF='$PHP_SELF?addscore=1&UserName=$UserName&logondetails=$logondetails'>" .
     
    "Add a Test Score!</A></P>");
     

    mysql_close($link);
    ?>
    </body>
    </html>

  2. #2
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    cheshire
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    More info on problem

    If I manually set userid to a value of say 2 then it works - the id column in the table gets a value of 2. There can't be that much wrong - its frustrating!

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    cheshire
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    more progress made!

    If I name the variable on the submit a score form something other than '$userid' then the column in the 'testscores' table called 'testscore' receives the value entered on the form. The only other problem now is after pressing submit as userid isn't getting assigned a value the script is displaying all the rows from the table that have a zero in the id column.

  4. #4
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    cheshire
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Another fudge!

    I can stop it displaying all the zero id entries after I press submit by putting the following before the line that displays the test name and test scores

    if ("SUBMIT"!==$addscore)

    This is just a patch and make do though!

  5. #5
    Victory shall be mine tubedogg's Avatar
    Join Date
    Mar 2001
    Location
    Medina, OH
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd try adding userid=$userid to your URL like this

    Code:
    echo("<P><A HREF='$PHP_SELF?addscore=1&UserName=$UserName&logondetails=$logondetails&userid=$userid'>" . "Add a Test Score!</A></P>");
    Otherwise I can't see why that wouldn't work, unless it doesn't like the array with urlencode...
    Kevin


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •