PHP insert and get scope identity

59 Views Asked by At

im using PHP and SQL Server i have 2 tables: 1. user (Id_user, user_name) 2. purchase (Id_purchase, Id_user, description)

i want to in insert into "user" and then insert into "purchase" with the same Id_user in "user"

the first insert works, but the problem is in the second table insert, because of the scope identity this is my code

$sql = "INSERT INTO dbo.user(dbo.user.user_name)values('John') SELECT SCOPE_IDENTITY() AS Id";
            if($result = sqlInsert($dbc, $sql)){
                foreach($result as $r){
                    $myScopeId = $r['Id'];
                }
                $sql = "INSERT INTO dbo.purchase(dbo.purchase.Id_user,dbo.purchase.description)values(".$myScopeId.",'description example')";
                if($result = sqlInsert($dbc, $sql)){
                    $success = "success!";
                }
                else{
                    $warning = "error";
                }   
            }
            else{$warning = "error";}
0

There are 0 best solutions below