「別々のテーブルに同時insertを考える」の編集履歴(バックアップ)一覧はこちら

別々のテーブルに同時insertを考える」(2015/11/19 (木) 00:11:40) の最新版変更点

追加された行は緑色になります。

削除された行は赤色になります。

*概要どうしてもテーブルを分けたい場合について考えます。 テーブルを別に挿入したいけど、紐づけするためにidも挿入したい。そんな場合にauto_incrementで両方やっていいのか?という問題が出てきます。 色々と検索しても問題ないということなのだそうですが、小心者のため保険としてother1のauto_incrementのlastidを取得して、その値も一緒にother2のテーブルの方に挿入していく方法を考えます。11月18日記事 ~ ~ *テーブル設計 other1 CREATE TABLE other1( id INT NOT NULL AUTO_INCREMENT, col1 VARCHAR(64) NOT NULL, col2 INT NOT NULL, PRIMARY KEY(id) ) ~ other2 CREATE TABLE other2( id INT NOT NULL AUTO_INCREMENT, other1_id INT NOT NULL, col3 VARCHAR(64) NOT NULL, col4 INT NOT NULL, PRIMARY KEY(id) ) ~ *コード 超手抜きスクリプトにします。ご了承ください。クエリの方をご覧いただければと思います。 ~ index.php <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>別テーブルのインサート</title> </head> <body> <form action="complete.php" method="post"> <label>col1文字列<input type="text" name="col1"></label> <label>col2数字<input type="text" name="col2"></label> <label>col3文字列<input type="text" name="col3"></label> <label>col4数字<input type="text" name="col4"></label> <input type="submit" name="submit" value="挿入"> </form> </body> </html> complete.php <?php require ("config.php"); $dsn = 'mysql:dbname='.DB_NAME.';host='.DB_HOST.';port='.DB_PORT.''; $user = DB_USER; $password = DB_PASSWORD; try{ $dbh = new PDO($dsn, $user, $password); $stmt = $dbh -> prepare("INSERT INTO other1 ( col1 ,col2) VALUES (:col1, :col2)"); $stmt->bindParam(':col1', $_POST['col1'], PDO::PARAM_STR); $stmt->bindParam(':col2', $_POST['col2'], PDO::PARAM_INT); $stmt->execute();//実行 //ここで最後のauto_incrementのidを取得する $last_id = $dbh->lastInsertId();//このようなPDOでの使い方があります。 //別テーブル挿入 $stmt = $dbh -> prepare("INSERT INTO other2 ( other1_id,col3 ,col4) VALUES (:other1_id, :col3, :col4)"); $stmt->bindParam(':other1_id', $last_id, PDO::PARAM_INT); $stmt->bindParam(':col3', $_POST['col3'], PDO::PARAM_STR); $stmt->bindParam(':col4', $_POST['col4'], PDO::PARAM_INT); $stmt->execute();//実行 print "完了"; }catch (PDOException $e){ print('Connection failed:'.$e->getMessage()); die(); } ?> ~ ~ *まとめ PDOでは[[PDO::lastInsertId:http://php.net/manual/ja/pdo.lastinsertid.php]]というものが存在し、それを使えば取得できます。あとはその値をもう一つのテーブルの方にも挿入していけばいいと思います。 ~ ~ 以上
テーブルを別に挿入したいけど、紐づけするためにidも挿入したい。そんな場合にauto_incrementで両方やっていいのか?という問題が出てきます。 色々と検索しても問題ないということなのだそうですが、小心者のため保険としてother1のauto_incrementのlastidを取得して、その値も一緒にother2のテーブルの方に挿入していく方法を考えます。11月18日記事 ~ ~ 目次 #contents ~ ~ ---- ~ *テーブル設計 other1 CREATE TABLE other1( id INT NOT NULL AUTO_INCREMENT, col1 VARCHAR(64) NOT NULL, col2 INT NOT NULL, PRIMARY KEY(id) ) ~ other2 CREATE TABLE other2( id INT NOT NULL AUTO_INCREMENT, other1_id INT NOT NULL, col3 VARCHAR(64) NOT NULL, col4 INT NOT NULL, PRIMARY KEY(id) ) ~ *コード 超手抜きスクリプトにします。ご了承ください。クエリの方をご覧いただければと思います。 ~ index.php <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>別テーブルのインサート</title> </head> <body> <form action="complete.php" method="post"> <label>col1文字列<input type="text" name="col1"></label> <label>col2数字<input type="text" name="col2"></label> <label>col3文字列<input type="text" name="col3"></label> <label>col4数字<input type="text" name="col4"></label> <input type="submit" name="submit" value="挿入"> </form> </body> </html> complete.php <?php require ("config.php"); $dsn = 'mysql:dbname='.DB_NAME.';host='.DB_HOST.';port='.DB_PORT.''; $user = DB_USER; $password = DB_PASSWORD; try{ $dbh = new PDO($dsn, $user, $password); $stmt = $dbh -> prepare("INSERT INTO other1 ( col1 ,col2) VALUES (:col1, :col2)"); $stmt->bindParam(':col1', $_POST['col1'], PDO::PARAM_STR); $stmt->bindParam(':col2', $_POST['col2'], PDO::PARAM_INT); $stmt->execute();//実行 //ここで最後のauto_incrementのidを取得する $last_id = $dbh->lastInsertId();//このようなPDOでの使い方があります。 //別テーブル挿入 $stmt = $dbh -> prepare("INSERT INTO other2 ( other1_id,col3 ,col4) VALUES (:other1_id, :col3, :col4)"); $stmt->bindParam(':other1_id', $last_id, PDO::PARAM_INT); $stmt->bindParam(':col3', $_POST['col3'], PDO::PARAM_STR); $stmt->bindParam(':col4', $_POST['col4'], PDO::PARAM_INT); $stmt->execute();//実行 print "完了"; }catch (PDOException $e){ print('Connection failed:'.$e->getMessage()); die(); } ?> ~ ~ *まとめ PDOでは[[PDO::lastInsertId:http://php.net/manual/ja/pdo.lastinsertid.php]]というものが存在し、それを使えば取得できます。あとはその値をもう一つのテーブルの方にも挿入していけばいいと思います。 ~ ~ 以上

表示オプション

横に並べて表示:
変化行の前後のみ表示: