MySQL database setup help - php

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.

Related

How to store user ID of one person to other table in database?

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.

Users History and Users saved database schema design

I want to store into database the visited users and favourite users (when a user clicks on hearth icon). So, I could create 1 table, users_activities like so:
Users_activities:
id_users (pk)
id_users2 (pk)
activity (varchar or enum, it'll contain "history", "favourite") (pk)
created (datetime)
For the history.php or favourites.php pages, I'll select all, where id_users = X and activity = "history/favourite", join users table. For insert and update, I have a composite primary key, users, users2 and activity. If I'll find a users X, users2 Y and activity Z, update the row..else insert.
Otherwise, I could create 2 separated table, users_history and users_favourite, like so:
Users_history
id_users (pk)
id_users2 (pk)
created
users_favourite
id_users (pk)
id_users2 (pk)
created
it's a simple design and SQL is tiny simple than first form. Which is better? I use php and mysql.
"History" sounds like a log of things. "Favorites" sounds like a current set of values. They have two different sets of usages and properties; do not put them in the same table.

Database Schema, PHP

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

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.

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