Jump to content

Rating Photos


franzwarning

Recommended Posts

Assuming you have two tables, photo and user, and each has an id  (photo_id, user_id)  as the primary key, you put a table between the two.  Let's assume you call this table "photorating"

 

 

photorating
--------------
photorating_id (primary key, autoincrement)
users_id  (fk to users table)
photo_id (fk to photo table)
rating tinyint
created_on timestamp

 

You stick a row into this table for a rating.  Add a unique index on user_id, photo_id and it becomes impossible for a user to have more than one rating row in the table.  You have a variety of options for how you want to deal with the rating ... you can look up the rating, or simply try the update and handle the failure/exception that will occur due to the unique index.

 

 

Link to comment
https://forums.phpfreaks.com/topic/230887-rating-photos/#findComment-1188540
Share on other sites

It was just shorthand for me pointing out that the datatype and name should match the name of the primary key in each of the associated tables.  There are other benefits to defining a true foreign key constraint, but with mysql many people use the default myisam engine which has no support for constraints or foreign keys.

 

Often people will do this:

 

mytable

id (primary key)

 

 

anothertable

-----------------

id

mytable_id  (foreign key to mytable)

 

 

So the actual name doesn't have to be the same in each table.  Whatever name you use for it, the important thing is that the datatype of the column should be exactly the same.  Since you will also be joining on that table, it's important to have a non-unique index on it as well.

 

 

Link to comment
https://forums.phpfreaks.com/topic/230887-rating-photos/#findComment-1188753
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.