Web Analytics Made Easy -
StatCounter Help a nub POST - CodingForum

Announcement

Collapse
No announcement yet.

Help a nub POST

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help a nub POST

    I'm still learning the basics at the moment and am making a simple system to store grades and student details entered on a PHP page. Basically, i want some of the input to go to one table called "student" and other bits to go to a table called "grades". Both contain a field called "student_id", the one in the grades table is a foreign key of the one in the student table.

    I am using this code to POST information correctly:

    Code:
    $sql="INSERT INTO student (firstname, lastname, emailaddress, telnumber, username, password)
    VALUES
    ('$_POST[firstname]','$_POST[lastname]','$_POST[emailaddress]','$_POST[telnumber]','$_POST[username]','$_POST[password]')";
    
    
    if (!mysql_query($sql,$con))
      {
      die('Error: ' . mysql_error());
      }
    echo "Your record has been added!";
    
    $sql="INSERT INTO grades (maths, english, science, gradescore, student_id)
    VALUES
    ('$_POST[maths]','$_POST[english]','$_POST[science]','$_POST[gradescore]','$_POST[student_id]')";
    
    if (!mysql_query($sql,$con))
      {
      die('Error: ' . mysql_error());
      }
    echo "Your record has been added!";
    That inputs the data correctly and it goes to the correct table. However, when I enter a second piece of information, I get an error telling me it can't enter data into the grades table. This is because the primary key isn't auto updating and is still "0". Therefore, as it's set to unique, it can't have dupe fields.

    When data is posted to the student table, it correctly applies a new, unique primary key number called "student_id". How do I get it to input the exact same number into the "student_id" field in the grades table (student_id in the grades table is a foreign key of student_id in student table)

    I'm pretty sure i've explained this really badly, so just ask and i can provide more info
    Last edited by alex98uk; Apr 4, 2009, 09:05 AM.

  • #2
    As a little clearer explanation, here is data I entered. I basically want the student_id in both tables to be the same where I enter some information.

    Comment


    • #3
      student_id in the student table where is primary key must be auto_increment and after you insert a row in this table you can use mysql_insert_id to get the student_id and insert the value in the second table.

      http://www.php.net/manual/en/functio...-insert-id.php

      best regards

      Comment


      • #4
        Yeah, the student_id in the student table is set to AUTO_INCREMENT. I looked at the mysql_insert_id, but I couldn't make heads or tails of it!

        So, how exactly would I implement this into my own project?

        Code:
        mysql_query("INSERT INTO grades(student_id) values ('kossu')");
        printf("Last inserted record has id %d\n", mysql_insert_id());
        ?>
        So, I understand up to: mysql_query("INSERT INTO grades(student_id) but then how do I define which table and field to take the data from?

        Comment


        • #5
          Originally posted by alex98uk View Post
          Yeah, the student_id in the student table is set to AUTO_INCREMENT. I looked at the mysql_insert_id, but I couldn't make heads or tails of it!

          So, how exactly would I implement this into my own project?

          Code:
          mysql_query("INSERT INTO grades(student_id) values ('kossu')");
          printf("Last inserted record has id %d\n", mysql_insert_id());
          ?>
          So, I understand up to: mysql_query("INSERT INTO grades(student_id) but then how do I define which table and field to take the data from?
          mysql_insert_id get the value of the last inserted auto_inrement field in the last table, so you don't need to know in which table was inserted but you must fetch and use it asap, before another insert.
          the code lok like this:
          PHP Code:
          $query "insert into firsttable (student_id, ....) values('', ...)";
          $result mysql_query($query);
          if(
          $result){
            
          $myid mysql_insert_id(); // retrive before anything else happend
            
          $query "insert into secondtable (....,student_id,...) values(....,".$myid.",....)";
             
          // rest of processing and you can reuse $myid in how many query you need

          best regards

          Comment


          • #6
            Just at a curiosity, is this a homework assignment??

            Comment


            • #7
              Originally posted by sea4me View Post
              Just at a curiosity, is this a homework assignment??
              Yea, still in the stages of learning

              Comment


              • #8
                1.5) No homework assignments - Do not post your entire homework assignment and request that other members do it for you. This is considered cheating, and your thread may even be used by your school to prove your guilt. Now, you may ask for advice or help on a specific aspect of your assignment that you're having trouble with. Use common sense as far as what's acceptable in terms of soliciting help with homework assignments.
                Just a reminder.... don't ask too far....

                Also:
                2) Type in a subject that summarizes your question- A great way to turn off other members wanting to help you is to type an incomplete or silly subject for your post. Examples of poor subject titles include:

                - "Help!"
                - "I'm a newbie...be gentle"
                - "How can I..."
                - "Urgent...deadline tomorrow!"
                Just reminders......

                Comment

                Working...
                X