Reputation: 867
I am inserting data in two tables on my database. and i used LAST_INSERT_ID()
function
but i am getting this error
PHP Fatal error: Call to undefined function LAST_INSERT_ID()
this is my code:
$sql = "";
$sql .= "BEGIN; ";
$sql .= "INSERT INTO circle_call_prefixes (";
$sql .= " circle";
$sql .= " ,prefix";
$sql .= " ) VALUES (";
$sql .= " :cid";
$sql .= " ,:prefix";
$sql = "INSERT INTO circle_call_destinations (";
$sql .= " autoNo";
$sql .= " ,destination";
$sql .= " ,source_circle";
$sql .= " ) VALUES (";
$sql .= LAST_INSERT_ID();
$sql .= " ,:prefix";
$sql .= " ,:prefix";
$sql .= " COMMIT; ";
$sql .= " );";
$stmt = $dbh->prepare($sql);
foreach($Insert_array as $e1)
{
$stmt->bindValue(':circle', $e1['cid']);
$stmt->bindValue(':prefix', $e1['prefix']);
$stmt->bindValue(':comment', $e1['comment']);
$stmt->bindValue(':cid', $Cid);
$stmt->execute();
}
thanks
Upvotes: 0
Views: 1392
Reputation: 6065
LAST_INSERT_ID
is in SQL, try this:
$sql = "";
$sql .= "BEGIN; ";
$sql .= "INSERT INTO circle_call_prefixes (";
$sql .= " circle";
$sql .= " ,prefix";
$sql .= " ) VALUES (";
$sql .= " :cid";
$sql .= " ,:prefix";
$sql = "INSERT INTO circle_call_destinations (";
$sql .= " autoNo";
$sql .= " ,destination";
$sql .= " ,source_circle";
$sql .= " ) VALUES (";
$sql .= "LAST_INSERT_ID()";
$sql .= " ,:prefix";
$sql .= " ,:prefix";
$sql .= " COMMIT; ";
$sql .= " );";
$stmt = $dbh->prepare($sql);
foreach($Insert_array as $e1){
$stmt->bindValue(':circle', $e1['cid']);
$stmt->bindValue(':prefix', $e1['prefix']);
$stmt->bindValue(':comment', $e1['comment']);
$stmt->bindValue(':cid', $Cid);
$stmt->execute();
}
Upvotes: 2
Reputation: 790
Because LAST_INSERT_ID()
is a function of Mysql, you've put it as PHP function, sometime using string append will cause this kind of issues, it's better to use string block as below.
$sql = <<<SQL
BEGIN;
INSERT INTO circle_call_prefixes(circle ,prefix)
VALUES (:cid, :prefix);
INSERT INTO circle_call_destinations(autoNo, destination, source_circle)
VALUES (LAST_INSERT_ID(), :prefix, :prefix);
COMMIT;
SQL;
Upvotes: 0