INSERT to two different tables, but have the same identifier?

I have a database of Users and another table for Teachers. Teachers have all the properties as a user, but also an email address. When pasting into a database, how can I insert information so that the identifier is the same for both?

the identifier is currently in automatic restriction.

this is what i have at the moment:

$sqlQuery="INSERT INTO user(firstName,lastName,DOB,title,password,classRoomID) 
    VALUES('$myFirstName','$myLastName','$myDOB','$myTitle','$newPassword','$myClassRoom')";
    $result=mysql_query($sqlQuery);

$sqlQuery = "INSERT INTO teacher(email) VALUES ('$myEmail')";
$result=mysql_query($sqlQuery);

Thank you!

+4
source share
5 answers

. (T ( ) U ( ). U. .

  • , varchar
  • .

, , , , , .

+1
+3

:

$sqlQuery="INSERT INTO user(firstName,lastName,DOB,title,password,classRoomID) 
    VALUES('$myFirstName','$myLastName','$myDOB','$myTitle','$newPassword','$myClassRoom')";
    $result=mysql_query($sqlQuery);

    $id = mysql_insert_id();

$sqlQuery = "INSERT INTO teacher(id, email) VALUES (' $id ','$myEmail')";
$result=mysql_query($sqlQuery);
+1

id:

$last_id = mysqli_insert_id(); // or mysql_insert_id() if you're using old code

mysql LAST_INSERT_ID():

$sqlQuery = "INSERT INTO teacher(id, email) VALUES ((SELECT LAST_INSERT_ID()), '$myEmail')";
+1

LAST_INSERT_ID(), , .

teacher

id | user_id | email

, teacher.id anyting, user_id user.

InnoDB, ,

0

Source: https://habr.com/ru/post/1535544/


All Articles