简体   繁体   中英

PHP MySQL, get next key

I'm very new to PHP/MySQL.

I have a form that submits data into the following categories of my MySQL database:

DATE_INIT, PARTNO, QTY

I also have a column titled "ECRNUM" that is my primary key and auto-increments. After the data is placed in the database, I have a phpmailer that needs to include the primary key (ECRNUM) in the subject line. I'm trying to figure out how I can get the primary key of the data that was just put into the database. Can someone help? If its a SQL query, can someone guide me in the right direction of what syntax to use?

FYI I used PDO to input the data:

$sqlx = "INSERT INTO opendpu (DATE_INIT, PARTNO, QTY) VALUES (:datex, :partno, :qty)";
 $statement = $db->prepare($sqlx);
$params = array(
      ":datex" => $datex,
      ":partno" => $partno,
      ":qty" => $qty
      );

$statement->execute($params) or die(print_r($statement->errorInfo(), true));
$db = null;

}
  catch ( PDOException $exception )
{
  echo "PDO error :" . $exception->getMessage();
}

http://php.net/manual/en/pdo.lastinsertid.php

Syntax:

public string PDO::lastInsertId ([ string $name = NULL ] )

For example with a DB handler of $dbh :

print $dbh->lastInsertId(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM