Insert to more than one table with PHP:PDO on MSSQL
I'm trying to write a php function which is going to put values to two diferent tables, but it dsn't work. How should I do?
public function AddKursplanering($kursBudgetId, $momentId, $momTypID, $pId, $rollId, $tid, $utfall)
{
if($stmt = $this->m_database->GetPrepareStatement(" INSERT INTO Kursmoment(KBID, Moment开发者_开发技巧ID, MomTypID)
VALUES(:kursBudgetId, :momentId, :momTypID)
INSERT INTO Uppgift (KMID, PID, RollID, Tid, Utfall)
VALUES (@@IDENTITY, :pId, :rollId, :tid, :utfall)"))
{
$stmt->bindValue(':kursBudgetId', $kursBudgetId,PDO::PARAM_INT);
$stmt->bindValue(':momentId', $momentId,PDO::PARAM_INT);
$stmt->bindValue(':momTypID', $momTypID,PDO::PARAM_INT);
$stmt->bindValue(':pId', $pId,PDO::PARAM_INT);
$stmt->bindValue(':rollId', $rollId,PDO::PARAM_INT);
$stmt->bindValue(':tid', $tid,PDO::PARAM_INT);
$stmt->bindValue(':utfall', $utfall,PDO::PARAM_INT);
if($stmt->execute())
{
$stmt->CloseCursor();
return true;
}
return false;
}
}
- If you are getting an error, post the error text
- If it is not an error, but it is not working for some reason, state clearly what part doesn't work
Now, that should actually work, but use SCOPE_IDENTITY()
instead of @@IDENTITY
, which may return you an unrelated number if there are triggers involved.
Also, consider encapsulating such logic in a stored procedure with the parameters, so you can call a single proc that does both inserts (essentially the same code)
Without knowing the error message from the query above it look like you need to have a semi-colon between the statements. Without it, it will be interpreted as one query instead of two.
if($stmt = $this->m_database->GetPrepareStatement("
INSERT INTO Kursmoment(KBID, MomentID, MomTypID)
VALUES(:kursBudgetId, :momentId, :momTypID);
INSERT INTO Uppgift (KMID, PID, RollID, Tid, Utfall)
VALUES (@@IDENTITY, :pId, :rollId, :tid, :utfall)"))
精彩评论