SitePoint Sponsor

User Tag List

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

    id value doesn't get inserted into table!

    The script below is passed a UserName and logondetails from another script. This script then uses those details to insert a score on a test into the testscores table. The test name and test score are added to the table but a nought is added to the 'id' column.
    I know this script is getting the value for $userid as I can get it to print the value. I thought the form wasn't sending this value to the server so I used the hidden type of box to get it sent - but still I just get a zero in the id field. Completely stumped now!
    Can anyone see what is going on?
    <html>
    <head>
    <title>Add A Score 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");
    print(
    "$userid");

    print (
    "<FORM ACTION=\"$PHP_SELF\" METHOD=POST>\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 (
    "<INPUT TYPE=HIDDEN NAME=userid VALUE=\"$userid\">");
    print (
    "<p><INPUT TYPE=SUBMIT NAME=addscore VALUE=\"SUBMIT\">");
    print (
    "</FORM>\n");



    // 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>");
     }
    }
    ?>
    </body>
    </html>

  2. #2
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it may just be me, but since when do you put a @ sign in front of your mysql functions?
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*

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

    the @ bit

    The @ means the functions error messages are over-ruled and you can put a more friendly message in .
    Back to my script. I have noticed that 'Greetings Angie Graham!' becomes 'Greetings!' after submitting the form so the username variable isn't sent and so I expect userid isn't either. But I'm confused as the hidden box on the form should mean the userid and its value are sent shouldn't it?

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

    Here is all my work so far in case it helps to spot the error

    There are two tables:

    1. personaldetails
    5 columns in total
    column 1 is called userid and it is tiny int primary key autoincrement
    column 2 is called firstname and it is varchar 20
    column 3 is called surname and it is varchar 20
    column 4 is called password and it is varchar 10
    column 5 is teacherstatus and it is ENUM('Y','N')

    2.testscores
    4 columns in total
    column 1 is testid and it is tiny int primary key autoincrement
    column 2 is testname and it is varchar 20
    column 3 is testscore and it is medium int 3
    column 4 is id and it is tiny int (foreign key to personaldetails)

    The script that receives the personal details is:
    <HTML>
    <HEAD>
    <TITLE> Sample Page </TITLE>
    </HEAD>
    <BODY>
    PHP Code:
    <?php
    if ($Message == "Invalid") {
        print (
    "<B><CENTER><FONT COLOR=RED>The id and password you entered do not match what is on file. Please try again!</FONT></CENTER></B>\n");
    }


     if (isset(
    $firstname) AND isset($surname) AND isset($password) AND isset($userid))
    {
    $name=$firstname " " $surname;
    $logondetails=$userid " " $password;
    // Now we will welcome the user to our site.
    print("<P><b><center>Wellcome: $name </center></b><br> This is the feedback part of the \'Humans As Organisms\' module.<P>\n");

    $name=urlencode($name);
    $logondetails=urlencode($logondetails);

    print(
    "<P>Click here <A HREF= \"bestpassword5.php?name=$name&logondetails=$logondetails\">link</A>\n");
    print(
    " to be taken to a page that will display your test results.</P>\n");
    }

    else
    {
    print(
    " <!-- No name has been provided, so we
     prompt the user for one. -->"
    );

    print(
    " <FORM ACTION = $PHP_SELF METHOD = POST><br>\n");
    print(
    " Please enter your first name: <INPUT TYPE = TEXT  NAME = \"firstname\"><br>\n");
    print(
    " Please enter your surname: <INPUT TYPE = TEXT  NAME = \"surname\"><br>\n");
    print(
    " Please enter your userid: <INPUT TYPE = TEXT  NAME = \"userid\"><br>\n");
    print(
    " Please enter your password: <INPUT TYPE = PASSWORD  NAME = \"password\"><br>\n");
    print(
    " <INPUT TYPE = SUBMIT VALUE = \"GO\">\n");
    print(
    " </FORM><br>\n");


    }
    ?>
    </body>
    </html>

    the script that handles the info from the form is:
    PHP Code:
    <?php
    $Host 
    "***********";// Set the variables for the database access:
    $User "*****";
    $MasterPassword "*******";       
    $DBName "****";     
    $TableName "personaldetails";

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

    if (! @
    mysql_select_db($DBName) )
     {
     print(
    "<P>Unable to locate the $DBName database at this time.</P>\n");
     exit();
     } 
    $Array=explode(" ",$logondetails);

    $Query="SELECT * FROM $TableName WHERE password='$Array[1]' AND userid='$Array[0]'";
    $Result=mysql_query ($Query) or die(mysql_error());
    $num_rows=mysql_num_rows($Result);
    $Row mysql_fetch_array ($Result);

    if(
    $num_rows==1
    {
     if(
    $Row['teacherstatus']=='Y')
      {
      
    $Newstring=implode(" ",$Array);
      
    $logondetails=urlencode($Newstring);
    $name=urlencode($name);
      
    header("Location: teacher2.php?UserName=$name&logondetails=$logondetails");
      exit();
      }

        else
          {
          
    $Newstring=implode(" ",$Array);
          
    $logondetails=urlencode($Newstring);
    $name=urlencode($name);
          
    header ("Location: student3.php?UserName=$name&logondetails=$logondetails");
          exit();
          }
    }

    header ("Location: firstpage5.php?Message=Invalid");
    exit();         


    mysql_close ($link);

    ?>
    the script that I have been working on that your directed to if your a valid student is:
    <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 ("<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

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

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

    And the script that this takes you to is:
    <html>
    <head>
    <title>Add A Score 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");
    print(
    "$userid");

    print (
    "<FORM ACTION=\"$PHP_SELF\" METHOD=POST>\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 (
    "<INPUT TYPE=HIDDEN NAME=userid VALUE=\"$userid\">");
    print (
    "<p><INPUT TYPE=SUBMIT NAME=addscore VALUE=\"SUBMIT\">");
    print (
    "</FORM>\n");



    // 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>");
     }
    }
    ?>
    </body>
    </html>

    Thanks for wading through this lot.


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
  •