MySQL insert into with subquery (on a junction table) - php

Basically, I'd like to insert into a junction table, getting values from another one.
Like this:
$sql= "INSERT INTO cars_owners (car_id, owner_id ) VALUES ($id, SELECT owners.owner_id FROM owners WHERE owners.owner_name='$name'))";
However, this one does not work, anyone know why?

You're mixing the syntax for inserting a set of values with the syntax for inserting from a SELECT statement. There is no VALUES keyword when inserting from a SELECT.
INSERT INTO cars_owners
(car_id, owner_id )
SELECT $id, owners.owner_id
FROM owners
WHERE owners.owner_name='$name'

Related

Writing INSERT query for multiple tables

I am writing a recipe database app and I have the database schema sorted but I am not struggling to figure out what query to write to insert the recipe into the database.
I have 3 tables
ingredients
ingredients_id...
recipes
recipe_id ...
recipes (pivot table)
ingredients_id
recipe_id
but it was my understanding that you can only insert data into one table at a time.
This is where my problem lies. If I have a form to input a recipe how do I add the recipe which is split over 3 tables? Do i use multiple queries. If so how can I guarantee they will match.
Sorry if this sounds like a dumb question but it is frying my brain, thanks in advance for the feedback.
Best regards
You guarantee the rows are related to one another by assigning common values to the appropriate columns.
If you have decided to define the recipe_id column in recipes to be AUTO_INCREMENT, and you insert a row which has a generated value assigned to that column, you can use the LAST_INSERT_ID() function to retrieve the value assigned to the column
INSERT INTO recipes (recipe_id, col2, col3) VALUES (NULL, 'fee', 'fi');
If the insert statement succeeded, you can retrieve the value assigned to recipe_id column (in the same MySQL session, before you execute any other statements), by executing a statement like this:
SELECT LAST_INSERT_ID() AS recipe_id;
NOTE: Some MySQL client libraries have methods/functions that perform this for you, so you can retrieve the value without having to prepare and execute another SQL statement in your code, e.g.
PDO::lastInsertId ref: http://www.php.net/manual/en/pdo.lastinsertid.php
mysqli::$insert_id ref: http://www.php.net/manual/en/mysqli.insert-id.php
You could then supply the value returned in the INSERT to the association table, for example
INSERT INTO recipe_ingredient (recipe_id, ... ) VALUES ( 42, ... )

PHP MySql Insert columns form one table to another

I want to insert my table data from copying another table with some new given data.
I use this query
$sql = "INSERT INTO table2(name, city, email,money)
hasib,SELECT table1.city, table1.email,
newsletter_subscribers.email
FROM table1 WHERE name='jesy',100";
But its not Work
You must give all columns inside the select statement columns, even constant values, e.g.
insert into table2(name, city, email, money)
select 'hasib', city, email, 100
from table1
where name = 'jesy'
If you want to take values from multiple tables, as your select statement suggests, you must look into joins.

Insert the data from the data select in other table in PDO and MYSQL

There are two table
user:
id
uid
board:
id
message
user_id
The problem is , I got the uid now , and I would like to insert data to board, but I want to simplify all things to one query, so I tried
"INSERT INTO board (message,user_id) VALUES (:message, SELECT id FROM users WHERE uid=:uid)";
but in this way is not working, would anyone kindly teach me the right syntax? Thanks
Enclose SELECT in parentheses like this
"INSERT INTO board (message,user_id) VALUES (:message, (SELECT id FROM users WHERE uid=:uid))";
You either use a VALUES clause or a SELECT clause, not both.
INSERT INTO board (message, user_id)
SELECT :message, id
FROM users
WHERE uid = :uid

How to Insert values into one table from all tables from database

I have problems with the last line of the code:
if(isset($_POST['kolona']))
{
foreach($_POST['kolona'] as $vrednost)
mysql_query("ALTER TABLE tablica ADD $vrednost text NOT NULL");
mysql_query("INSERT INTO tablica ( ".(implode(',',($_POST['kolona']))).") SELECT ".(implode(',',($_POST['kolona'])))." FROM druga");
}
First query is making columns in table 'tablica' and second query suppose to insert values in that columns from all tables from which are the columns, for now it's just hard coded, it's only from table 'druga', but i don't know how to go through the all tables, not just 'druga'. I tried with a loop and also with implode function but nothing seems to be working. Can anyone help?
You can tackle merging problems like this using the ON DUPLICATE KEY feature:
INSERT INTO target (id, column1, column2)
SELECT id, column1, column2 FROM source
ON DUPLICATE KEY UPDATE column1=VALUES(column1), column2=VALUES(column2)
It works well provided you have a PRIMARY KEY column without conflicts between your source and target tables.

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