• Register
Welcome to Techno Smarter QA, Questions, Answers Community
Welcome to Techno Smarter Q&A, where you can ask questions and receive answers from other members of the community. Ask the tech & programming Question that you want. Guests Posting are allowed.
+5 votes
46 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 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 next form the data are--- 

in PHP by (320 points) 5 19
edited by
PLease check that question i edit it
Check your voucherno column name properly and please do not delete question description. Kindly improve your language.
voucherno    varchar(10)    utf8_general_ci        No    None

Propertie of voucherno
Check your insert queries.
$result = mysqli_query($cser, "INSERT INTO voucher_i(type,voucherno,date) VALUES('$type','$Voucherno','$Date')");
        $result1 = mysqli_query($cser, "INSERT INTO  voucher_m(voucherno) VALUES('$Voucherno')");
   if($result){

This is a query for voucher_i table insertion and also voucher_m table only voucherno insertion

        $result = mysqli_query($cser, "INSERT INTO voucher_m(mainhead,subhead,dr,cr) VALUES('$mainhead','$subhead','$dr','$cr')");
this is query for voucher_m in another MYSQL table
Why are you using three queries? Two queries with the same variable but different values.if you want to insert data in two tables then create only two queries.
You can try this -
$result = "INSERT INTO table_name1 and values ...; INSERT INTO table-_name2 and values...;";

mysqli_multi_query($cser, $result);

4 Answers

+3 votes

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 . 
by (5.1k points) 4 16 45
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
Check my other answer. You have added three queries with two same tables.
+3 votes

If you want to insert data in two tables so why added 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 query.

/insert data to database

$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. 

by (5.1k points) 4 16 45
please check the question i edit it
It's same with three queries.
Kindly check again.
+3 votes

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 also same for table 2. In that case, you will able to insert data into multiple tables using PHP and MYSQL multi-query.  

by (3.1k points) 1 13 38
+2 votes

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. 

by (7.1k points) 4 21 46
I want to insert data in both MYSQL table eg voucherno so how i do this
...