MySQL insert to multiple tables (relational) - php

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.

Related

How to link a row of MySQL database table to another row in another table

I know it makes little sense... and i'm new to using MySQL...
What i'm trying to do here is, link one tables row to another tables row...
for an example there are two tables..
one table is for user registration and same table is used for login as well...
and the next table is for user posts.. like status updates and all...
here is how i want it...
user_log_info:-
id ( primary )
firstname
lastname
username
email
password
posts:-
id ( primary )
userposts
posted_by
date_post
so as you can see, i want the user_log_info tables username to be automatically copied to posts posted_by row... And i have no idea how i can archive this...
You haven't given nearly enough information to give a full answer, but I'll do my best with what you've given.
Tables
+-----------------+ +-----------------+
| users_log_info | | posts |
+-----------------+ +-----------------+
| int ID (primary)| | int ID (primary)|
+-----------------+ | int posted_by |
+-----------------+
(I left off fields that are irrelevant to what you seem to want to do, I'm just simplifying it)
posted_by is an unofficial foreign key, or referencing the primary key of another table.
To insert, what you can do is along the lines of this:
INSERT INTO posts(...., posted_by) VALUES (...., user.ID)
Where .... is referencing all of your other information to insert
Then, to find information on someone who posted something:
SELECT * FROM users_log_info WHERE ID = Post.posted_by
Or if you want to find all posts by a user:
SELECT * FROM posts WHERE posted_by = user.ID
So, if Bob, who is User ID 3 wants to post "Hi", you might be able to do:
INSERT INTO posts(content, posted_by) VALUES('Hi', bob.ID)
And then when you are outputting the post you might do this:
post = (however you choose the post to put on the page)
userPosted = SELECT * FROM users_log_info WHERE ID = post.posted_by
print post.content + " posted by: " userPosted.Name
Essentially, the field "posted_by" is, to "posts" an arbitrary number, but you know that it links to, or references, a user. It does that by referencing "ID", which is the primary key of users_log_info, so that when you want to get information from users_log_info, is all you need to do is select the entry which has the ID that corresponds to "posted_by". I do recommend naming it something like posterID, however, for easier identification.

Update table without primary key

Im building a shop and each product has a unique set of attributes.
the product db looks like this:
products_id, relation_id, product_name, description,
price, glass, shipping, img, cat, subcat, model
Since every product has several (~40) different attributes unique to that product only ive created a second table to store them.
products_id, att_name, att_val, att_head, att_standard, att_order
This works fine, because there will never be two unique rows.
The problem, however, is when i need to modify the attributes content.
using MySQL Workbench i can modify a row using something like
UPDATE product_attributes SET att_val='1500' WHERE products_id='112' AND att_head='threshold'
This however, doesn't seem to work when i update from my php script.
Is there an easy way to modify the table to support updating?
Im well aware of the stupidity not having an unique column.
But im not sure how to make the two tables relate.
Where should i store the unique id of the attributes?
One choice,
add a primary key "auto_incremented" into the product_attributes table...
ALTER TABLE `product_attributes` ADD `id` INT( 10 ) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST
This Id is just for CRUD (Create Read Update Delete) task.
The only relation you can have between your two tables is the products_id wich allow you to have few product_attributes for one product
Since 1 product has more than 1 unique attributes that u store in a second table, you should use the ID of the table product and store it in the second table with the attributes.
Hope this is what u need?

assign product to a user mysql

I have a list of products and users. I'm trying to assign products to a specific user. How should I set it up in database. I don't need php code for now as I have no idea how to connect the data in MySQL? I'm planning to use check-box pass the post info for the db.
Thanks,
You need a many-to-many relationship between your product and user table as #Dragon suggested. Let's say you have table call user_products with two fields userId and productId.
Now you can assign as many product to a person by inserting an entry in this new table, e.g. userId is 100 and productId is 10,11,12
INSERT INTO user_products (userId,productId) VALUES (100, 10);
INSERT INTO user_products (userId,productId) VALUES (100, 11);
Hope you got an idea!
In your products tables add a user_id field which will contain the 'id' field of the users table.
For example
id | user_id | product_name | product_desc
If you wanna select the products based on specific user
SELECT * FROM products WHERE user_id = 1;

simple update of foreign key in php mysql website

hi i cannot update a table which has foreign keys on it. in this table, instead of displaying the primary keys of the foreign key, i choose to display their names: this is a simple diagram:
Here are my foreign tables:
Size Table:
sId sName
1 1x1
2 2x2
Brand Table:
bId bName
1 brand1
2 brand2
Supplier Table:
sId sName
1 supp1
2 supp2
So here is my Warehouse Table using a join statement:
pId pName pSize pBrand pSupplier
1 prod1 1x1 brand1 supp1
2 prod2 2x2 brand2 supp2
here is my edit in php and mysql form:
########### EDIT PRODUCT
if(isset($_POST['editproduct'])){
$product_id=$_POST["product_code"];
$product_name=$_POST["product_name"];
$size_name=$_POST["size_name"];
$brand_name=$_POST["brand_name"];
$supplier_name=$_POST["supplier_name"];
$sql = "UPDATE warehouse SET
product_name='$product_name'
,size_id='$size_id'
,brand_id='$brand_id'
,supplier_id='$supplier_id'
WHERE
product_code='$product_code'";
$result=mysql_query($sql,$connection) or die(mysql_error());
header("location: warehouse.php");
} ?>
the weird thing is that the first try i edit the table it does not error. but for the second time, it prompts me the error of foreign key constraint, :(
i have a feeling that because i use a join statement in my warehouse table, that conflicts my update query since the one i am updating is the primary key and i only display its name.
you reference the variables $size_id, $brand_id, $supplier_id in your SQL statement but never set them anywhere..
Though if you say that the edit works the first time, I'm guessing you might not have posted all your code?

Insert Mysql with PHP - problem with foreign key insert

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

Categories