There are two insert requests.

As for the second request, get the value of the id field, which is formed as a result of the first one. The field is set to AUTO_INCREMENT .

Query to the first table:

 $login = ($_POST['login']); $email = (trim($_POST['email'])); $password = md5(md5(trim($_POST['password']))); $sql = "INSERT INTO users (login, paroli, email) VALUES (:login, :password, :email)"; $sqlData = $pdo->prepare($sql); $sqlData->bindParam(':login', $login, PDO::PARAM_STR); $sqlData->bindParam(':password', $password, PDO::PARAM_STR); $sqlData->bindParam(':email', $email, PDO::PARAM_STR); $sqlData->execute(); 

Query to the second table:

 $sql = "INSERT INTO images_ava (id) VALUES (:id)"; $sqlData = $pdo->prepare($sql); $sqlData->execute(); 
  • 2
    PDO :: lastInsertId - is it? - UserName
  • like yes, and help please insert it? - Koly
  • After the first execute() insert $lastId = $db->lastInsertId(); where $db connect to base. And use $lastId in another request. - UserName
  • thanks, everything works, give me the answer, I will give you balls there - Koly
  • sometimes it makes sense to wrap both inserts in a transaction. - teran

1 answer 1

In order to get the identifier of the last inserted record, you can use the lastInsertId function.

In this case, it needs to be called after the first execute call:

 $lastId = $db->lastInsertId(); // $db коннект к базе. 

You can then use $latsId in subsequent queries.

PDO :: lastInsertId