I'm trying to find out if it's possible to insert the data from one table to another table if someone submits an ID that is indexed in the two tables.
For example:
I have Table A with: ID | First Name | Last Name | Course
and Table B has: ID | Name | Course
Table A has data: 32 | John | Doe | IT
And Table B will receive its data from a Form that a user will input.
How do I automatically insert the First Name and Last Name column from Table A into Table B if the user inputs #32 on the form as well which is indexed with Table A ID column?
I'm trying to use the normal Insert Into and Selecting the table columns for the Values of the name but it doesn't seem to read it.
$upload = "INSERT INTO `TableB` ( `ID`, `Name`, `Course`) VALUES ('$id','(SELECT FirstName FROM TableA WHERE ID = '$borrow_id'),' '$course');";
I was thinking that maybe I can select the ID from the submitted form and if there is a similar data in Table A, it will get the other data from it and insert it into Table B.
Your logic is almost there, you just got the syntax wrong. You can use an INSERT..SELECT statement on TableB selecting the data you want from TableA, so it should look something like this:
<?php
$upload = "INSERT INTO `TableB` ( `ID`, `Name`, `Course`) SELECT $id, FirstName, $course FROM TableA WHERE ID = '$borrow_id;";
To test, you can just copy the SELECT part of your query and execute on your database to see if the data is correct. Keep in mind that this statement should have the exact number of columns in your insert and be in the same order.
Related
I have a weird problem.
I have a rather large database with two tables. I need to change a column's contents from a name to an ID that already exists in another table.
Example:
I have a table that contains a column "Name"
the name column has the persons "lastname, firstname" as shown
Name | othercolumn
Smith, John |
I would like to change the contents of the name column to the staffID associated with the persons name.
The staff table is
staffID | firstName | lastName
1 john smith
My end result should be
Name | othercolumn
1 |
I've tried all sorts of joins and concats, but can't seem to get it down with my limited mysql knowledge. Is there a way to do this without having to do it manually? The comma seems to give me alot of grief. Thanks!
You need to be very careful about this. First, I assume that StaffId is a number. So, add a column to the table:
alter table t add StaffId int;
Then, update this column:
update t join
staff s
on t.name = concat_ws(',', s.lastname, s.firstname)
set t.StaffId = s.StaffId;
Note that after you have done this, you may still have StaffId values that are NULL:
select t.*
from t
where t.StaffId is null;
These are the names that are not in the staff table. They require more work. When you are done, you can drop the name column.
i need to insert some data into the table 'companies' with columns :
company_id | company_name
and at the same time (from the same form) into another table 'contact_persons' :
contact_name | company_id
where the company_id must be the value from 'companies' table where company_id is a PK and AI.
Is it possible to do that in ONE single step instead of inserting first the company_name and then reading the table 'companies' and retrieving the 'company_id' to insert it into the second table ('contact_persons')?
I'm not sure if that is possible, but it would be much more elegant and efficient...
Thanks in advance.
You can do it using LAST_INSERT_ID() to get the last auto increment id from Companies table and inserting the same in other table. something like
INSERT INTO companies (company_name) VALUES ('test');
SET #last_id_companies = LAST_INSERT_ID();
INSERT INTO contact_persons (contact_name, company_id)
VALUES ('test', #last_id_companies);
Not in a single statement, but in a single transaction, so they are both executed at the same time and rolled back.
START TRANSACTION;
--Your statements here
COMMIT;
Is it possible to display the mysql table using php, i mean display the records with field names without specifying the row names.
example
|id|name |address |status | <--- this is the field in the mysql table
|1|name1 |address1 |status1| <-- this is the records
and more records...
Just an idea:
Run DESCRIBE tablename and get field names/type
Run SELECT * FROM tablename and get records
If I understand you correctly you want to display all records in the table?
$sql = mysql_query("SELECT * FROM tabel")
the star means you will select all rows. change table to your table name..
Now all your table information is stored in $sql
For instance, my main query inserts a new row into table REF_STORES which has a structure like this:
ID | Store_Name | VariableA | VariableA+VariableB | VariableC+VariableA
In My Case, I am pulling each one of these fields from one of two different tables. ID and Store Name come from one, while the others (VariableA, VariableB, and VariableC) come from another. What I cannot figure out is how to pull all these variables from the two different tables and simultaneously calculate, then insert the results into the table structure above.
Do you already have a SELECT query that will pull the values from both tables simultaneously? If you do, you can do something like:
INSERT INTO REF_STORES (id, store_name, var_a, var_ab, var_ca)
SELECT id, store_name, var_a, var_a + var_b, var_c + var_a
FROM [however you're joining the two tables]
[project_select]
UserID (fk) | project_id (fk) | project_category_id (fk)
[project_category]
project_category_id | category
[projects]
project_id | projectname
[project_user]
UserID | Name
How can I insert Data with php in the tables project_category, projects and project_user, to get automatically the values in the project_select table with the FK's?
Update:
How can I merge this 3 queries into one line?
INSERT INTO project_category
VALUES (1,'Fruits')
INSERT INTO projects
VALUES (4,'Apple')
INSERT INTO project_user
VALUES (2,'Adam')
and get this values with this one query in the project_select table:
[project_select]
UserID (fk) | project_id (fk) | project_category_id (fk)
2 4 1
You can use the mysql_insert_id function to retrieve the id you just inserted.
mysql_query("INSERT INTO Persons VALUES (4,'Nilsen', 'Johan', 'Bakken 2', 'Stavanger')");
$person_id = mysql_insert_id();
So, do inserts on your tables, call mysql_insert_id() after each insert, store inserted IDs and finally use the IDs in the mysql command to create the join table.
I doubt that I understood your problem correctly. But if you want to have the values entered automatically in project_select table? Then how about using MySql triggers? But be sure you are well aware of the database internals before using it.
What are triggers?, Setting up triggers
But, please describe this -
problem with foreign
key insert?