I tried to use the following code:
$last_id = Connection::connect() -> lastInsertId('name');
to get the id for the last inserted item but does not work. Anyone could help identify the issue?
<?php
class Connection{
static public function connect(){
$link = new PDO("mysql:host=localhost;dbname=ham", "root", "");
$link -> exec("set names utf8");
return $link;
}
}
static public function mdlAddAccount($tableOne, $dataOne, $tableTwo, $dataTwo){
$stmtTwo = Connection::connect() ->prepare("INSERT INTO $tableTwo(name, planLevel) VALUES (:name, :planLevel)");
$stmtTwo -> bindParam(":name", $dataTwo["name"], PDO::PARAM_STR);
$stmtTwo -> bindParam(":planLevel", $dataTwo["planLevel"], PDO::PARAM_STR);
$last_id = Connection::connect() -> lastInsertId('name');
$stmtOne = Connection::connect()->prepare("INSERT INTO $tableOne(name, user, password, profile, status, relatedAccountID) VALUES (:name, :user, :password, :profile, :status, :relatedAccountID)");
$stmtOne -> bindParam(":name", $dataOne["name"], PDO::PARAM_STR);
$stmtOne -> bindParam(":user", $dataOne["user"], PDO::PARAM_STR);
$stmtOne -> bindParam(":password", $dataOne["password"], PDO::PARAM_STR);
$stmtOne -> bindParam(":profile", $dataOne["profile"], PDO::PARAM_STR);
$stmtOne -> bindParam(":status", $dataOne["status"], PDO::PARAM_STR);
$stmtOne -> bindParam(":relatedAccountID", $last_id, PDO::PARAM_STR);
#$stmt -> bindParam(":photo", $data["photo"], PDO::PARAM_STR)
if ($stmtOne->execute() && $stmtTwo->execute()) {
return 'ok';
} else {
return 'error';
}
$stmt -> close();
$stmt = null;
}