Insert Mysql with PHP - problem with foreign key insert - php

[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?

Related

MYSQL Inserting Data Into Table Using Indexed Table

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.

Reset id values in existing table (without deleting records)

let me say that I have a table in my phpmyadmin that looks like this:
id | name
1 | John
2 | Dave
5 | Tiffany
As U can see by id - i've deleted 2 records between 'Dave' and 'Tiffany'.
My question:
Is there a way to 'reset' or repopulate the id so that the 'Tiffany' record would have id=3 and so on ?
The only way to start counting id again I've found is a 'TRUNCATE' but it deletes all my records which I dont want
I do this sometimes when I create a new table and want to get 'clean' ids. If there is no reference to / connection with other table, you can do:
SET #var:=0;
UPDATE `table` SET `id`=(#var:=#var+1);
ALTER TABLE `table` AUTO_INCREMENT = 1;
Do not do this for ids already used in other tables!

Inserting a new record into mysql tables and adding new records with same id for multiple tables

I am designing a Diabetes management system and my question is about inserting a new record in two of the tables. I have a user table with the fields:
user_ID (PK), Username, password, etc
I also have the profile table, which stores personal information about a user. Profile:
User_ID (FK from user table), Firstname, LastName, Address, etc.
I have stored the user_id in the profile table as a foreign key because i want to have a user associated with their own profile. E.G. a person with username grover with User_Id '3' will have a profile with personal info and the user_ID in the profile table will also be '3'. I hope the above makes sense. My question is, when i have stored the data from the form into variables, how would i write the query so that the new record will be created and the right fields will be stored in the correct table with the new record having the same user_ID in both the user and profile table.
Try looking at LAST_INSERT_ID function. This will give you the PK of the record you just entered, ie your user_id which you can use in the corresponding join tables
insert into Users values (null,'asdf','asdf',3,1);
Query OK, 1 row affected (0.00 sec)
mysql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
| 1 |
+------------------+
1 row in set (0.00 sec)
There you go

MySQL insert to multiple tables (relational)

tbl_product
Name | Creator | UID | Salerank
tbl_price
Supplier | Price | UID
I want to insert a product and then insert multiple prices into a seperate table. How is it possible to ensure that both tables had the same UID ideally an auto increment field? I will be using PHP alongside MySQL.
Thanks,
J
Make UID an auto_increment primary key on the products table, but just a regular primary key on the prices table (no auto_increment). After you insert itnto products, use the PHP command mysql_insert_id(). This will get the ID generated from the last query, which will be your UID generated on the products table. Assign it a variable and use it in your insert statement on the prices table.
http://php.net/manual/en/function.mysql-insert-id.php
Use a GUID for the UID, or better, insert your products and the insert the prices using e.g. the name of the product (assuming unique) to look up the relevant product UID.

Should I use AI on these fields in MySQL?

I have this db below. I wonder how I should use the ID to identify each record.
Everything is connected from the classified_table!
Two Questions:
Should I use AI on every PK in this case?
Could somebody give me the FULL code for selecting an entire classified from only an ad_id ("bmw_330ci_8939483" for example)?
I am new to normalized db and making a good database work, so detailed instructions is very much appreciated... Also if you notice any 'wrongs' in this db please let me know.
category table:
cat_id (PK)
cat_name
category_options table:
option_id (PK)
cat_id (FK)
option_name
option_values table:
value_id (PK)
option_id (FK)
value
classifieds table:
classified_id (PK)
ad_id (VARCHAR) something like "Bmw330ci_28238239832" which will appear in URL
poster_id (FK)
cat_id (FK)
area_id (FK)
headline
description
price
etc....
posters table:
poster_id (PK)
name
email
tel
password
area table:
area_id (PK)
area
community
Thanks
I would auto-increment (AI) on fields that I would do majority of searching by. AI makes it easier to return results, but there are performance issues where it can slow down the database.
In regards to the query, I am not exactly sure what you would want to return, but this query returns the classified_id by the given ad_id
SELECT classified_id FROM classifieds_table WHERE ad_id = "bmw_330ci_8939483"
To perform a single insert into your classifieds table and column ad id the value audi a4 would be:
INSERT INTO classifieds_table ad_id VALUES "audi_a4"
Or multiple inserts using the same table, multiple fields and multiple values would be:
INSERT INTO classifieds_table (ad_id, poster_id) VALUES ("audi_a4", 10)
Notice I left out classified_id because if you choose to auto-increment it will automatically assign a value without you explicitly assigning one.
Check out MySQL :: Building a Database-Driven Website using PHP and MySQL for more tutorials.
Using auto-increment for your PKs sounds sensible, because it sounds like you already want to use a surrogate key and auto-increment makes the inserts very straightforward. Worth taking a look at this discussion about how to pragmatically choose what primary key to use.

Categories