3 Replies Latest reply: Nov 21, 2006 1:53 AM by Newsgroup_User RSS

    Last_Insert_ID()

    Anhialator
      I have this exact situation, but with no PHP MySQL code to read so I can figure out what to do (how to write the code for these 3 tables). I'm a beginner, self-taught, and I've been working on this for months, with no success. I've purchased and read (and worked through) PHP for Dreamweaver 8 by Dave Powers, but this isn't covered in there:

      CREATE TABLE students (
      student_id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
      firstname VARCHAR(50) NOT NULL DEFAULT '',
      lastname VARCHAR(50) NOT NULL DEFAULT '',
      sex ENUM('M','F') NOT NULL DEFAULT 'M',
      dob DATE NOT NULL DEFAULT '0000-00-00'
      );

      CREATE TABLE classes (
      class_id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
      instructor_id INT(11) NOT NULL DEFAULT '0',
      name VARCHAR(50) NOT NULL DEFAULT '',
      hours INT(1) NOT NULL DEFAULT '0'
      );

      CREATE TABLE students_classes (
      student_id INT(11) NOT NULL DEFAULT '0',
      class_id INT(11) NOT NULL DEFAULT '0',
      semester ENUM('FALL','SPRING','SUMMER') NOT NULL DEFAULT 'FALL',
      year INT(4) NOT NULL DEFAULT '2005',
      grade INT(1) DEFAULT NULL,
      PRIMARY KEY (student_id,class_id,semester,year)
      );

      My thinking might be illogical since I don't understand how (students_classes.student_id) receives its value from |students|, while maintaining a NOT NULL value in (students_classes.class_id). How does a value from a field in |students| or |classes| get into |students_classes| without violating the NOT NULL conditions of one of its fields?

      Please, help me with this. I'm sinking deeper into agoraphobia over this. :)

      Thank you for your consideration.

        • 1. Re: Last_Insert_ID()
          Newsgroup_User Community Member
          Anhialator wrote:
          > How does a value
          > from a field in |students| or |classes| get into |students_classes| without
          > violating the NOT NULL conditions of one of its fields?

          I don't understand what you mean by "without violating the NOT NULL
          conditions". Basically, you have to insert the student_id and class_id
          into students_classes when you're creating a new record. In your insert
          and update record forms, you need to have two drop-down menus: one for
          the student's name, the other for the class name. Store the student_id
          as the value for each <option> in the first one, and the class_id in the
          second one.

          Since you have worked through "Foundation PHP for Dreamweaver 8" (thank
          you), you build these drop-downs in the same way as for the authors in
          Chapter 9.

          --
          David Powers
          Adobe Community Expert
          Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
          http://foundationphp.com/
          • 2. Re: Last_Insert_ID()
            Anhialator Community Member
            Dave,
            You're brilliant! I have no idea if what you suggested in your reply indeed works, as, I found a solution, somehow, (or was it a work-around?). Anyway, I just wanted to thank you for that Foundations book! It's my bible! And for that, I say, "You're brilliant"!

            Thank you!
            • 3. Re: Last_Insert_ID()
              Newsgroup_User Community Member
              Anhialator wrote:
              > Dave,
              > You're brilliant! I have no idea if what you suggested in your reply indeed
              > works, as, I found a solution, somehow, (or was it a work-around?). Anyway, I
              > just wanted to thank you for that Foundations book! It's my bible! And for
              > that, I say, "You're brilliant"!
              >
              > Thank you!

              Thank *you*. I've been trying to tell people for years that I'm
              brilliant, but they take no notice. Finally, I've found someone who
              agrees. ;-p

              Seriously, though, I'm glad you found the book useful. Now all I need to
              do is to convince another few thousand to break open their wallets and
              buy a copy.

              --
              David Powers
              Adobe Community Expert
              Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
              http://foundationphp.com/