How to combine 3 MySQL table inserts - php

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

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.

How can i insert data in table with a join sql?

i have 2 tables on db.
First table:
users
id|name|mail|password|group
second table
scores
id|name|score
The idea is get the name from users using the id (this id already know because i get that by php), then insert a score in table scores using the name obtained by a id.
I suppose that can i do with a inner join between users and scores.
How can i do that?
Insert into scores(id, name, score)
select ID, name, score(that you can pass)
from users
where id = (parameter pass by PHP)
Agree. You can simply use this:
INSERT INTO scores values (null, (select name from users where users.id= 1),100);
Replace the score and id with the values you get.
Assume you have the auto increment for your ids.

Insert into two related tables

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.

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>

MYSQL / PHP - merge two tables with different columns

I want to access a database merging two tables from a php script in order to output the result to an html table. Both tables are pretty much the same except one column I need only exists in one table. I don't have direct access to the database.
INSERT INTO globalusers
(surname, firstname, email, location)
INSERT INTO localusers
(surname, firstname, email)
Location exists only in globalusers. I need to use this column as a dropdown filter in my table and add the column location with the string "local" to the data coming from the localusers table.
Here's a fiddle
So the expected result would be one table with four columns and the string "local" in the location column for the localusers table.
What is the select that I need?
MySQL version: 5.5.36
Sounds like you're looking to combine the results of the two tables. If so, you can use UNION ALL for this:
SELECT surname, firstname, email, location
FROM globalusers
UNION ALL
SELECT surname, firstname, email, 'local'
FROM localusers
Updated SQL Fiddle

Categories