试试这个。有关分步说明,请参见 cmets。 (未经测试!)
// Construct query, using positional variables (?).
$sql =
'INSERT INTO ' .
'`auth_table` ' .
'( `ID`, `Password`, `Login`, `AuthType`, `IdentityNo` ) ' .
'VALUES ' .
'(?, pwdencrypt(?), ?, ?, ?)';
// Create prepared statement from query.
$statement = $pdo->prepare($sql);
// Bind parameters by position, the index of which is 1-based.
$bindIndex = 1;
// Increase index on every line. Enforce type as we go.
$statement->bindValue($bindIndex++, $id, PDO::PARAM_INT);
$statement->bindValue($bindIndex++, $plaintextPassword, PDO::PARAM_STR);
$statement->bindValue($bindIndex++, '0', PDO::PARAM_STR);
$statement->bindValue($bindIndex++, 1, PDO::PARAM_INT);
$statement->bindValue($bindIndex++, '7700000000000', PDO::PARAM_STR);
$statement->execute();
// This is your @@identity
$userNum = $pdo->lastInsertId();
// Second insert.
$sql =
'INSERT INTO ' .
'`charge_auth` ' .
'(`usernum`, `type`, `expiredate`, `payminutes`) ' .
'VALUES ' .
'(?, ?, DATEADD(day, ?, getdate()), ?)';
$statement = $pdo->prepare($sql);
$bindIndex = 1;
$statement->bindValue($bindIndex++, $userNum, PDO::PARAM_INT);
$statement->bindValue($bindIndex++, 0, PDO::PARAM_INT);
$statement->bindValue($bindIndex++, 1000, PDO::PARAM_INT);
$statement->bindValue($bindIndex++, 0, PDO::PARAM_INT);
$statement->execute();
// Third insert.
$sql =
'INSERT INTO ' .
'`CashDB..cashaccount` ' .
'(`id`, `UserNum`, `Cash`, `CashBonus`, `UpdateDateTime`) ' .
'VALUES ' .
'(?, ?, ?, ?, GETDATE())';
$statement = $pdo->prepare($sql);
$bindIndex = 1;
$statement->bindValue($bindIndex++, $id, PDO::PARAM_INT);
$statement->bindValue($bindIndex++, $userNum, PDO::PARAM_INT);
$statement->bindValue($bindIndex++, 0, PDO::PARAM_INT);
$statement->bindValue($bindIndex++, 0, PDO::PARAM_INT);
$statement->execute();