×

Please Login or Register to continue.

5
(6.8K Views)

Hello Everyone! 
In one HTML form, when I enter voucherno so why it is not going to another table automatically because they have same voucherno column.

I want to insert data into two database table using PHP. Is there any solution for multiple table data insertion?

I created two tables in MYSQL. I have the same column name in both tables. I need a solution for insert data into multiples tables using PHP and MYSQL database

My first MYSQL database table structure.

image

This is Table 1 having column voucherno as connect to another table &

Second table columns -

image

That is table 2 having the same voucherno .

I tried this insert code for multiple tables -


//insert data to database
        $result = mysqli_query($cser, "INSERT INTO voucher_i(type,voucherno,date),voucher_m(voucherno) VALUES('$type','$Voucherno','$Date')");
        $query1 = "INSERT INTO voucher_i(type,voucherno,date)  VALUES('$type','$Voucherno','$Date')";
        $query2 = "INSERT INTO  voucher_m(voucherno) VALUES('$Voucherno')";
       mysqli_query($query1, $cser);
       mysqli_query($query2, $cser);
   if($result){
     header("location:Combined.php");

 

I edited it. When I applied the below query then the result is stored in the database table like this 

The data is showing me like this -

When I enter submit data in the next form the data are--- 

 

(90 Points)
in PHP

Share

4 Answers
(9.4K Points)
2

Thanks for posting here.

You have mentioned two tables with the same column. If you insert the data using HTML form, the data goes to one table and you want to insert the same data into both tables. I am not getting your purpose but if you want to insert data in both MYSQL tables, you should specify both table name and column name. I think you have specified the first table name. In PHP, you can use insertion one by one using a table name or insert query.   

Specify the insert query for the second table like as first table and define your second table column name in which you want to store data. 


Comment
I want to insert data in both MYSQL table eg voucherno so how i do this
osama 16 Sep 2019

(7.3K Points)
3

Welcome to techno smarter QA.

In PHP, you can insert data from an HTML form into multiple tables. There is no issue with multiple tables of data insertion. You want to insert data into two tables using PHP and MYSQL table. Yes, it is possible through the one table data insertion process. 

Follow this process - 

$query1 = "INSERT INTO table1 ...";
$query2 = "INSERT INTO  table2...";
mysqli_query($query1, $cser);
mysqli_query($query2, $cser);
Change table 1 and table 2 according to your table name and use values.It will help you to insert data in two tables . 

Comment
Warning: mysqli_query() expects parameter 1 to be mysqli, string given in E:\xampp\htdocs\Working\Combined.php on line 292 Warning: mysqli_query() expects parameter 1 to be mysqli, string given in E:\xampp\htdocs\Working\Combined.php on line 293 FailedData added successfully. errror
osama 16 Sep 2019
Check my other answer. You have added three queries with two same tables.
vishalrana1 16 Sep 2019

(7.3K Points)
(edited)
3

If you want to insert data in two tables so why add three queries. 

$result = mysqli_query($cser, "INSERT INTO voucher_i(type,voucherno,date),voucher_m(voucherno) VALUES('$type','$Voucherno','$Date')");
        $query1 = "INSERT INTO voucher_i(type,voucherno,date)  VALUES('$type','$Voucherno','$Date')";
        $query2 = "INSERT INTO  voucher_m(voucherno) VALUES('$Voucherno')";

If you want to add data into two-column then create only two MySQL queries.

$query1 = "INSERT INTO voucher_i(type,voucherno,date)  VALUES('$type','$Voucherno','$Date')";
        $query2 = "INSERT INTO  voucher_m(voucherno) VALUES('$Voucherno')";
       mysqli_query($query1, $cser);
       mysqli_query($query2, $cser);

 Try it. 

 


Comment
please check the question i edit it
osama 16 Sep 2019
It's same with three queries.
vishalrana1 16 Sep 2019
Kindly check again.
osama 16 Sep 2019

(4.3K Points)
3

Inserts data into multiple tables using PHP. It's possible using mysqli_multi_query. In. It helps to create multiple queries in one. You can use mysqli_multi_query for both tables. 

Let's have a look.

$result = "INSERT INTO table1  ...; INSERT INTO table2 ...;"; 
mysqli_multi_query($cser, $result);

 

Use table 1 and their values are also the same as table 2. In that case, you will be able to insert data into multiple tables using PHP and MYSQL multi-query.  

 


Comment

Related Questions :-

Featured Items:-


Certificate system in PHP website | PHP scripts

$22



Home Credito loan finance management multipurpose system in PHP

$65



Result management system with Marksheet in PHP website | PHP Scripts

$39




Your Answer

You can post your answer after login..