php - Is there anyway to get the primary key before a TRANSACTION is submitted? -
actually think of scenario this:
let's managing library stores many books.
our web application library allows user add books shelf.
database has 2 tables, books , authors. schemas these:
create table books (book_id int not null identity(1,1), book_name nvarchar(100) not null, author_id int not null, primary key (book_id), foreign key ( author_id ) references authors(author_id),) create table authors (author_id int not null identity(1,1), author_name nvarchar(100) not null, primary key (author_id))
assume request author name , book name store book on shelf.
automatically generate entry author if there no such author. want operation transaction.(i want rollback if goes wrong.)
can primary key before ending transaction this?
$server_name = "s3"; $connection_info = array( "database"=>"bis_testing", "uid"=>"bisuser", "pwd"=>"111"); $conn = sqlsrv_connect( $server_name, $connection_info); sqlsrv_begin_transaction( $conn ) $sql = "insert author(author_name) values (?);"; $author_name = 'dr. pro'; $stmt1 = sqlsrv_query( $conn, $sql, array($brand_name)); **// author primary key $author_pk** $sql = "insert book(book_name, author_id) values (?,?);"; $book_name = 'writing works'; $stmt2 = sqlsrv_query( $conn, $sql, array($book_name, $author_pk)); if ($stmt1 && $stmt2) { echo 'done'; }
if not, how should job?
i don't see way last insert id in mysql, can last inserted id sql server discussed here: best way identity of inserted row?
you stored procedure need do, although don't know how call these functions.
Comments
Post a Comment