I have the mysql tables
Contract
-**contract_id** (PRIMARY)
Contract_to_groups
-**group_id** (PRIMARY)
-contract_id
Groups_to_users
-group_id
-user_id
I have this multiselect(multiselect.js) where elem is the users
I have already the contract and I want to create a group of users. So I select my users from multiselectand then click submit. On submit in my table Groups_to_users should be saved the users I selected from the multiselect.
I have the problem that if I set in Contract_to_groups the group_id as AUTO_INCREMENT I cannot save in Groups_to_users the users for this group because I dont know the group_id of Contract_to_groups, it is AUTO_INCREMENT and especially if I have four(4) multiselects and each with users how can I save in these two(2) tables Contract_to_groups, Groups_to_users the data with the correct ids ?
Try to modify your Contract_to_groups table, and add an id which wil be the auto incrementing primary key:
-**id** (PRIMARY)
-group_id
-contract_id
Related
I have 2 tables in database user_info and product_info.I save user_id,name,email,etc in user_info and product_id,product_name,product_description in product info.Now I have to look for a particular product so how do I get which product was uploaded by whom.How to link user_info with product_info in Android
For this, you have two approaches
you need to create a pivot table having three fields as table user_product_info with fields: id , user_id and product_id. You can set user_id and product_id which is related. here id is a primary key which you can use for further things.
or you can create a field user_id in product_info table and can use that.
Hope it helps you.
I am developing a reservation system for my school project. It is based on PHP and MYSQL.
The system allows a user to register. After successful login, the users can make a reservation which is stored in the following table in phpmyadmin.
User Table
When a user registers, he gets a user_id. When a user makes a reservation, the data is inserted into the table against the same user_id. But when the user tries to make another reservation, there is no way to store the information of the next reservation.
The question is how do I allow the user to make several reservations?
The user_id is my primary key so I understand it is not possible to create multiple records against one primary key.
Do I have to create a new reservation table and link it to user_id through a Foreign key relationship>? But if my reservation table has a primary key, then several records cannot be inserted against one primary key.
Somehow, each reservation must be linked the unique user_id so that the user can check all the bookings under his name.
Each reservation also needs a unique ID which can be used to cancel/update the reservation.
I believe it must be a basic MySQL question. Something like creating a new reservation table and connecting it with user_ID but I am unable to think of a concrete solution.
Your help is very much appreciated.
Thank You.
Make table users
then at least 2 fields id, name
then make table reservations and its One-To-Many relation so you need to put foreign key in reservation(many side)
reservation
id, name, user_id
then to get reservations from user 1 use select with join
SELECT * FROM reservations r JOIN user u ON r.user_id = u.id WHERE u.id = 1
I need a help with a PHP/MySQL issue. I have a table named users and other named relationships.
users
--------------
id (PK)
name
email
etc
relashionships
--------------
id (PK)
id_user (FK to users.id)
id_friend (FK to users.id)
rating
I'm trying to INSERT multiple relationships but I don't want duplicated entries. I want to ignore the current row if the row is duplicated. I can't use the IGNORE statement because the id_user and the id_friend columns aren't unique. A user/friend may have multiple relationship rows.
Any tip?
You can create a unique key on the id_user/id_friend tuple. Neither of them are unique, but their combination is.
See multiple column indexes on the documentation.
Thanks amenadiel, I found that solution here and worked for me!
CREATE UNIQUE INDEX relation ON relationship (id_user, id_friend)
On my website, I have a table movies and a table users
I'm trying to have an "Add to favs" button that a user can click, which will add that movie to his favorites (ajax / javascript not necessary at the moment, just php).
So what's the simplest way I could do something like that? I've thought about this but I can't seem to find a solution (all I think of is way too complicated, and in my opinion not possible).
What's your thoughts?
I don't need a ready-made script, just an idea that could get me working (although if you have an example of such script, I'd be happy to look at it).
Thanks!
This is a many-to-many relationship. A user can favorite many movies, and a movie can be favored by many users. In an RDBMS, you represent a many-to-many relationship with a third table. I call this an intersection table but it goes by other names too.
Create a table with two columns. The columns are both foreign keys, referencing movies and users, respectively.
CREATE TABLE Favorites (
user_id INT NOT NULL,
movie_id INT NOT NULL,
PRIMARY KEY (user_id, movie_id),
FOREIGN KEY (user_id) REFERENCES Users(user_id),
FOREIGN KEY (movie_id) REFERENCES Movies(movie_id)
);
When a user chooses to favorite a movie:
INSERT INTO Favorites (user_id, movie_id) VALUES (?, ?)
When a user decides they don't like a movie any longer, delete the corresponding row:
DELETE FROM Favorites WHERE (user_id, movie_id) = (?, ?)
To get the set of movies favored by a given user:
SELECT movie_id FROM Favorites WHERE user_id = ?
To get the set of users who favor a given movie:
SELECT user_id FROM Favorites WHERE movie_id = ?
Regarding one of your comments:
You shouldn't make the "Add to favorite" a link. Indexers like Google will follow links, and then before you know it, every user has favorited every movie.
The general best practice is that read-only operations can be GET requests, while operations that write to the database can be POST requests. This means that you need to use a <form> element to submit POST requests, not an <a href="..."> element.
Add a third table:
CREATE TABLE user_favorites (
user_id INT NOT NULL,
movie_id INT NOT NULL,
PRIMARY KEY (user_id, movie_id),
FOREIGN KEY user_id REFERENCES users (user_id),
FOREIGN KEY movie_id REFERENCES movies (movie_id)
)
This is called an intersection table or join table, as it joins rows in the users table to rows in the movies table (as you see, each column is a foreign key). It is also defines a many-to-many relationship, because one user can like many movies and one movie can be liked by many users.
When you go to add a favorite movie for a user, all you have to do is insert a row in this table with the ID of the user and the ID of the movie:
INSERT INTO user_favorites(user_id, movie_id) VALUES([user ID], [movie ID])
To see what movies a user has favorited:
SELECT movie_id FROM user_favorites WHERE user_id = [user ID]
You will need to create a new table:
user_favorite_movies
--------------------
ID (primary key)
userID (foreign key)
movieID (foreign key)
date
Then when the user clicks the 'Add Favorite' button, you just insert a new row into user_favorite_movies with the users ID from the user table, the movie id from the movie table, and the date it was added (good for sorting later).
Hope this helps!
Best,
-Eric
You could create a table favourites with three columns, id, mid and uid. To add a favourite:
INSERT INTO favourites (mid, uid) VALUES (3, 5)
To search for favourites of one user:
SELECT * FROM favourites WHERE uid = 7
To search for people who favourited one movie:
SELECT * FROM favourites WHERE mid = 9
So far as I can see, you'll still need to use JavaScript or Ajax to do the post, unless you want to refresh the page every time thet mark/unmark a favorite, and also to add/remove the new favorite indicator in place at the same time.
Or am I missing something?
I am making a classifieds website...
I have these 6 tables:
Every category has sub-categories (or options) which you can see below.
Lets say the user wants to post a classified, and has entered all info into the forms necessary, and I am at the stage where I have to create the PHP code to actually INSERT the data into the database.
I am thinking something like this:
mysql_query("INSERT INTO classifieds (classified_id, ad_id, poster_id, cat_id, area_id, headline, description) VALUES ($classified_id, '$ad_id', $poster_id, $cat_id, $area_id, '$headline', '$description')");
But I don't know where to take it from here...
I think the posters table should not be like this, because how should I determine what the poster_id should be? Or should I set it to auto-increment?
Remember this, posters may not log in or anything, so there is no problem with one person having multiple poster_table records if you know what I mean.
classified_id is a random unique value generated by PHP so that is always unique.
Please guide me! I don't know how to link the tables together correctly.
If you have any Q let me know and I will update this Q!
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
You've got the right idea already. When someone creates a post, and enters their personal info, FIRST insert the "poster" record into the posters table. The "poster_id" primary key for that table should be an auto_increment field.
Next, get the ID of the new poster you just created using PHP's "mysql_insert_id". That integer value will be the number you put in the "poster_id" foreign key field in the "classifieds" table.
You should usually set the primary key to an auto-increment field.
When you have linked tables and you need to join on the id, you can first insert into the main table and then use the function mysql_insert_id to retrieve the id of the element you just inserted. You can then insert into the other table using this value as the foreign key.
This is a very standard way to do things, so it should be fine for you.