Hi friends,

I need to implement the "video rating" option in my website.
Its some thing like "*****" should be used to rate the video.
Any idea how to implement this feature ?

Thanks,
Satees

Dani AI

Generated

A concise, production-ready approach: store each vote server-side, validate and clamp values, prevent duplicate voting, and expose a small API that the star UI calls. asked for a simple 1-5 star rating; showed a quick client click idea (good for prototypes), and pointed to related threads on UI. The following focuses on reliable storage, security, and UX that work years later.

A simple normalized schema (adjust types to the project) and rationale:

CREATE TABLE video_ratings (
  id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  video_id INT UNSIGNED NOT NULL,
  user_id INT UNSIGNED DEFAULT NULL,
  rating TINYINT NOT NULL,
  ip VARBINARY(16) DEFAULT NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  UNIQUE KEY ux_video_user (video_id, user_id),
  INDEX idx_video (video_id)
) ENGINE=InnoDB;
  • Keep user_id for logged-in users and use the unique index to prevent duplicates. Anonymous votes need a separate scheme (cookie token or hashed identifier) because NULLs do not enforce uniqueness. Storing ip in binary with INET6 conversion is compact but check privacy rules before storing IPs.

Server-side flow (validate, then insert or upsert). Example using PDO and an upsert so logged-in users update their previous vote atomically:

$stmt = $pdo->prepare(
  'INSERT INTO video_ratings (video_id, user_id, rating)
   VALUES (:video, :user, :rating)
   ON DUPLICATE KEY UPDATE rating = VALUES(rating), created_at = CURRENT_TIMESTAMP'
);
$stmt->execute([':video'=>$videoId, ':user'=>$userId, ':rating'=>$rating]);

Compute display values with an aggregate query:

SELECT AVG(rating) AS avg_rating, COUNT(*) AS votes
FROM video_ratings WHERE video_id = :video;

Operational tips: clamp rating to 1..5 server-side, use prepared statements to avoid injection, return JSON for AJAX frontends and provide a POST fallback for non-JS clients, and consider caching aggregates in the videos table for high read volumes (update via transaction or background job). Prevent abuse by requiring login for one-vote-per-user, using cookie tokens for anonymous users, rate-limiting endpoints, and logging suspicious bulk activity. Finally, consider GDPR/privacy implications of storing IPs and apply anonymization or retention policies as required.

Recommended Answers

All 2 Replies

uh, hyperlinks with values assigned?
eg:

<a href="something.php?rate=1">*</a>
<a href="something.php?rate=2">*</a>
<a href="something.php?rate=3">*</a>
<a href="something.php?rate=4">*</a>
<a href="something.php?rate=5">*</a>

then when the page loads. check the value of rate and do with it what you will.
ie:

$rate = $_GET['rate'];
...
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.