Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

How to save data to Two or More MySQL Table with a Single Query

Featured Replies

Hello, I went through all the links available in Google but didn't found any correct solution. Please help the noob here. Also if you find that I am thinking totally in a wrong way then please suggest the correct way. I want to know that how thing is actually done.

 

I have created two tables so that I can do RDBMS (Data Normalization and Joining).

Here is my code:

<?php
require("conn.php");
 
// Escape user inputs for security
$name = mysqli_real_escape_string($conn, $_POST['name']);
$desc = mysqli_real_escape_string($conn, $_POST['desc']);
$cars = mysqli_real_escape_string($conn, $_POST['cars']);
$gender = mysqli_real_escape_string($conn, $_POST['gender']);
$bike = mysqli_real_escape_string($conn, $_POST['bike']);
$cycle = mysqli_real_escape_string($conn, $_POST['cycle']);
 
// attempt insert query execution
$sql = "INSERT INTO tbl_project2 (Name, Description, Cars, Gender, Own) VALUES ('$name', '$desc', '$cars', '$gender', '$bike' )";
$sql = "INSERT INTO tbl_project2b (Cycle) VALUES ('$cycle' )";

if(mysqli_query($conn, $sql)){
    echo "Records added successfully.";
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($conn);
}
 
// close connection
mysqli_close($conn);
?>

Whatever I try nothing works. I tried mysqli multi_query but its also not working. My code only saves data to the 2nd table and not in the 1st table. So what is the correct method of adding data simultaneously into two or more tables?

Thank you.

Edited by dsouravs

  • 3 weeks later...

You can use stored Procedure. Wrap the insert statements in stored procedure and call that procedure like

 

create procedure insert_multiple

as

begin

insert into table1...

insert into table2...

end

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.