[PHP][MySQL] How to insert data in 2 tables at same time?
You can get the value of tb1_id
using mysqli_insert_id()
, and then insert that into tb2
:
$sqla = "INSERT INTO tb1 (tb1_title, tb1_cat) VALUES ('$tb1_title', '$tb1_cat')";
if (mysqli_query($db, $sqla)) {
$tb1_id = mysqli_insert_id($db);
$sqlb = "INSERT INTO tb2 (tb2_title, tb2_doc, id_tb1) VALUES ('$tb2_title', '$tb2_doc', $tb1_id)";
mysqli_query($db, $sqlb);
}
Mysql insert into two tables at same time
You could use mysqli::multi_query
$query = "insert into games (games_title,games_date,games_author,games_image,games_keywords,games_link,games_content) values ('$games_title','$games_date','$games_author','$games_image','$games_keywords','$games_link','$games_content');";
$query .= "insert into archive (title, pub_date) values ('$games_title','$games_date')";
if (mysqli_multi_query($connect, $query)) {
echo "Data in two tables successfully inserted.";
}
Trying to Insert data to multiple tables in single query in php
Thomas i try your query u should modify and try this query:
Code :
$sql = "INSERT INTO contacts (firstName,lastName,nickName,cellNumber,homeNumber,workNumber) VALUES ($firstName,$lastName,$nickName,$cellNumber,$homeNumber,$workNumber) " ; "INSERT INTO address(street,city,state,country) VALUES($street,$city,$state,$country)"; "INSERT INTO contacts (email,birthday,memo)
values($email,$birthday,$memo)";
I think it should be must helpful.
How to use Insert query with multiple tables at a same time?
No, you can't insert into multiple tables in one MySQL command. You can however use transactions.
Check this : MySQL Insert into multiple tables? (Database normalization?)
Related Topics
Sending Message in Telegram Bot With Images
How to Stop PHP Code Execution
How to Restrict User from Downloading or Saving Pdf File
Join Category and Subcategory in Same Table
Trim Country Code from Phone Number in PHP
Mysql Combine Two Columns into One in Query
Php How to Loop Through a Post Array
How Should a Model Be Structured in MVC
Grabbing the Href Attribute of an a Element
How to Download an Mp3 File from Remote Url
How to Convert Text to Svg Paths
Merging Two Json by Adding Their Value
Telegram Bot Api: How to Send a Photo Using PHP
Calculate Difference Between 2 Times in Hours in PHP
Checking If Email Is in MySQL Database in PHP
Auto Update Cart on Quantity Change in Woocommerce Cart Page