Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将相同的标识列添加到不同的表中?_Php_Sql Server_Pdo - Fatal编程技术网

Php 如何将相同的标识列添加到不同的表中?

Php 如何将相同的标识列添加到不同的表中?,php,sql-server,pdo,Php,Sql Server,Pdo,我的MSSQL中有以下存储过程 declare @UserNum as int insert into auth_table( ID, Password, Login, AuthType, IdentityNo ) values(@id, pwdencrypt(@password), '0', 1, '7700000000000' ) set @UserNum = @@identity insert into charge_auth(usernum, t

我的MSSQL中有以下存储过程

declare @UserNum as int

    insert into auth_table( ID, Password, Login, AuthType, IdentityNo ) 
    values(@id, pwdencrypt(@password), '0', 1, '7700000000000' )

    set @UserNum = @@identity


    insert into charge_auth(usernum, type, expiredate, payminutes)
    values(@UserNum, 0, DATEADD(day, 1000, getdate()), 0)
    
    insert into CashDB..cashaccount (id,UserNum,Cash,CashBonus,UpdateDateTime) values(@id,@UserNum,0,0,GETDATE())

    select @UserNum as usernum
我希望能够将其转换为PDO PHP,我成功地完成了第一次插入。问题是所有表中都有一个持久的
UserNum
值。当我将数据插入第一个表时,会生成这个
UserNum
,它是一个自动递增的值。我想同时将其传递给其他表,以便它们通过该值进行连接

我不知道如何使用PHP和PDO实现这一点


提前谢谢。

试试这个。请参阅注释以了解逐步的解释。(未经测试!)


您好,前两个正在工作,但由于某种原因,第三个插入没有向CashDB添加任何内容。不过,我有一个输入错误。它现在正在工作。非常感谢。伟大的如果你觉得我的答案有帮助,请考虑将它标记为“接受”:对不起,不同意,但我所看到的只是一个赞成票(你的?)。接受答案意味着勾选复选标记,然后该复选标记变为绿色。我不想提起这件事,但我们都在努力提高自己的声誉。
// 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();