Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2015

    Unanswered: how autopopulate the child table.

    so i have just founded out that i can use the lastInsertId to get the last inserted id from the user table which i can use to get the autoincremented id. so half of the problem is already solved here is the code.

    $insert_into_database = $db->prepare("INSERT INTO login(username,password) VALUES('$this->username','$this->password')");


    $id = $db->lastInsertId();

    so i got that now, so basically half of the problem is solved, what i want to do now is to insert that $id into the child table, which is going to connect each user to a message in a many to many relationship. the child table is on another page and when i do this it's not working.

    $insert_info_into = $db->prepare("INSERT INTO info( id, message) VALUES('$id', '$this->message')");


    i am getting this error. Notice: Undefined variable: id in C:\xampp\htdocs\sandbox\practice\config.php on line 44

    so how can i do this can i have a trigger or something which i don't really know how to do.


    | user_id | username | password |

    | message_id | id | message |

    //id(fk)references user_id

  2. #2
    Join Date
    Nov 2004
    out on a limb
    Provided Answers: 59
    so what is line 44 in your script C:\xampp\htdocs\sandbox\practice\config.php

    if its
    PHP Code:
    $insert_info_into $db->prepare("INSERT INTO info( id, message) VALUES('$id', '$this->message')"); 
    then you need to drop in and out of PHP when referring to associative arrays. eg:-

    PHP Code:
    $SQLStatement "INSERT INTO info( id, message) VALUES('$id', '".$this->message."')"
    $insert_info_into $db->prepare($SQLStatement); 
    personally I prefer to assign SQL to a variable then use that variable in functions / method calls. the reason begin you can quickly separate out what the SQL through the debugger or even a simple echo statement

    I also tend to prefer to explcitly drop in and out of PHP when assignign variables in thign l,ike SQL, but thats just me. So if it were my code I'd use

    PHP Code:
    $SQLStatement "INSERT INTO info( id, message) VALUES('".$id."', '".$this->message."')"
    $insert_info_into $db->prepare($SQLStatement); 
    Last edited by healdem; 10-12-15 at 05:51.
    I'd rather be riding on the Tiger 800 or the Norton

Tags for this Thread

Posting Permissions

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