Insert into two related tables - php

tbl_account
username | password | status
tbl_data
username | name | address | phone
how can i insert data into both tables with php?
I tried with query
INSERT INTO tbl_account(username, password, status)
OUTPUT INSERTED.'bobo', 'Bobo Ali','Markt St. 26', '0979877'
INTO tbl_data(username, name, address, phone)
VALUES ('bobo','bobo123','manager');
but it doesn't work at all. The process is success but the table is remain empty.

You will need seperate queries for inserting the same data into multiple tables.
If you are having trouble with that, the process is simple:
Use the query to insert the data into the first table
Free the last result using mysqli_free_result() (or mysql_free_result() if you are using the MySQL API.
Reuse the same query you used in step 1. to insert the data into the second table
Code:
<?php
INSERT INTO tbl_account(username, password, status)
VALUES ('bobo', 'Bobo Ali','Markt St. 26', '0979877');
// Free the result here
INSERT INTO tbl_data(username, name, address, phone)
VALUES ('bobo','bobo123','manager');
// Free the result again, so you can use the query again if needed
?>

Use transactions. Alter your database like in the example and make userid primary key (auto increment)
tbl_account
userid | username | password | status
tbl_data
userid | username | name | address | phone
BEGIN;
INSERT INTO tbl_account(username, password, status)
VALUES ('bobo','bobo123','manager');
INSERT INTO tbl_data(username, name, address, phone)
VALUES(LAST_INSERT_ID(),'bobo', 'Bobo Ali','Markt St. 26', '0979877');
COMMIT;
Check also this example if you want an example of php-MySQL(transaction) implementation.
PHP + MySQL transactions examples

you can only do with 2 seperate queries
INSERT INTO tbl_account(username, password, status)
VALUES ('bobo', 'Bobo Ali','Markt St. 26', '0979877)'
Insert INTO tbl_data(username, name, address, phone)
VALUES ('bobo','bobo123','manager');

Yes, you need to have two separate queries.
But the answers are a little bit misleading because the table tbl_account has only three columns whereas the provided code inserts values for four columns.
So it should be:
INSERT INTO tbl_account(username, password, status)
VALUES ('bobo','bobo123','manager');
INSERT INTO tbl_data(username, name, address, phone)
VALUES ('bobo', 'Bobo Ali','Markt St. 26', '0979877');
Or you can write a procedure which these two inserts does.

Related

keeping optional(null) and required(not null) data in the same table or separate tables?

I have two sets of inputs in a product delivery form -
recipient information(required) - (country, state, city, zipcode, street, contact_no)
alternate_recipient_information(Optional) - (country, state, city, zipcode, street, recipient name, contact_no)
I need an efficient and flexible database schema and here's what I thought might work for me:
user
id, name, email
country
id, country, iso_code
state
id, state, country_id
city
id, city, state_id
address_type
id, type
1 required
2 optional
address
id, street(null), city_id(null), state_id(null), country_id(null), type_id
recipient
id, user_id, address_id, contact_no
alternate_recipient
id, name, address_id, contact_no
delivery_info
id, recipient_id, alt_recipient_id(null)
OR should I just use two separate tables for the required and optional sets of data or am i good to go? are there any better solutions?
In speaking about efficiencies, by splitting the table, you would have to JOIN tables to get the relevant data which is less efficient than having a single table with NULL values that you don't have to worry about. If you are pulling the data later, just use IFNULL and give it a value.
Making two separate tables will help you to make your structure more normalised but you have basic user profile , so you can continue with the present structure itself.

Join table insert and update separately

I have a registration form and 3 database table. Which is users, security,activity. In user there is name,email In security there is token and tokenExpire and in activity there is ip,system.
I want to make insert file where if user submit form it fill that each tables as based places.
Let's consider,
Name=$name | Email=$email | Ip=&ip | token=$token | tokenExpire= $time | system=$system.
I am insert connection like this :
$con->query("INSERT INTO users (Name,Email) value('$name','$email'). system(token,tokenExpire)value('$token','$time).activity(ip,system)VALUE('$ip','$system' ");
Is this correct format?
No, you cannot insert into multiple tables in one MySQL command.
Please try this, I hope it will help you
INSERT INTO users (Name, Email)
VALUES('test', 'test');
INSERT INTO system (user_id,token, tokenExpire)
VALUES(LAST_INSERT_ID(),'token','tokenexpirevalue');
INSERT INTO activity (user_id,ip, system)
VALUES(LAST_INSERT_ID(),'ip','system');

How to combine 3 MySQL table inserts

So, I'm creating a user system in php. And of course users need to be inserted in my MySQL table. Now, I've got three tables that need to be filled:
- emails
---- id, email, subscriber
- addresses
---- id, street, street_number, zip_code, city, country, phone, btw_number, company
- users
---- id, username, password, salt, surname, name, joined, email_id, permissions_id, address_id, address_2_id
I could insert the email query, then select it's ID (auto inc) as id1. After that INSERT address and SELECT the id (auto inc) and then combine those 2 to INSERT in the users table. But is there a more quicker way so that I don't have to select the id each time after I've inserted the data?
There is a mysql function for that.
You can do the First user-insert and After that you can use LAST_INSERT_ID().
that Seems to be exactly what you need.
For More informations you can go here
And here you got some code examples

PHP MySQL insert data from one table to another table

I want to fetch data from one table to another, i have took references from many sites and Stackoverflow but I wasn't able to solve error.
The last field is the applicant field where I would like to send Default value 'No'.
I want to do this whole thing in single query
insert into user_identity (login_no, customer_id, prename,
fullname, mobile, dob, age, applicant) values
select login_id, customer_id, c_prename, CONCAT_WS(' ',`c_firstname`,`c_lastname`),
c_mobile, dob, age, 'No' from customer where id = '1'
the keyword VALUES is not needed,try this..
insert into user_identity (login_no, customer_id, prename,
fullname, mobile, dob, age, applicant)
select login_id, customer_id, c_prename, CONCAT_WS(' ',c_firstname,c_lastname),
c_mobile, dob, age, 'No' from customer where id = '1'
refer this.. http://dev.mysql.com/doc/refman/5.1/en/insert-select.html
When we are inserting from another table, we do not use the keyword values as we are not providing the values implicitly. we are fetching it from another table.
So in that sense, to create a table as an exact replica of another we can do something like this
CREATE TABLE EMP as SELECT * FROM EMPLOYEE
Similarly while inserting we do something like this
Insert into <tablename>(column name) select (column name) from <Table name>

PHP Getting Previous MySQL Query's ID For Next Queries, How?

I have a simple question about MySQL and PHP here. Let's say I have this PHP syntax :
mysql_query("INSERT INTO Persons (FirstName, LastName, Age)
VALUES ('Peter', 'Griffin',35)");
on that Person table, there's a column named ID (Auto Increment). how to get Peter Griffin's ID after INSERT process is done without making another SELECT query?
or is it possible to do INSERT for 2 tables using single query? for example I want to INSERT Peter's address as well on Address table :
mysql_query("INSERT INTO Address (City, State, Zip)
VALUES ('Cupertino', 'California', 35212)");
that's all..
$new_id = mysql_insert_id();
Put that right after your INSERT query and it will give you the id.
I would't recommend trying to do two INSERTs in one, and the mysql_insert_id() method will make it simplest for you and your code.

Categories