Skip navigation
Anhialator
Currently Being Moderated

Last_Insert_ID()

Nov 19, 2006 4:34 PM

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.

 
Replies
  • Currently Being Moderated
    Nov 20, 2006 5:47 AM   in reply to Anhialator
    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/
     
    |
    Mark as:
  • Currently Being Moderated
    Nov 21, 2006 1:53 AM   in reply to Anhialator
    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/
     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points