Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6881197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:04:38+00:00 2026-05-27T05:04:38+00:00

I am using the below code to insert ratings for specific songs on my

  • 0

I am using the below code to insert ratings for specific songs on my application.

I am recording the songID, the rating given to the song and the userID who has voted on the song.

What I want to do is to prevent a user from voting if they have ‘already’ voted on a specific song. Therefore I need to be able to check if a row exists in the table before insertion.

So… If userID = 1, songID = 1 and rating = 4. This should insert fine.

If subsequently, an insertion attempt is made for userID=1, songID=1, rating=*, it should fail to insert

However if the user is voting on a different song… that should be allowed and the insertion should happen.

Any ideas how I would go about this?

//Add rating to database

if(!empty($_POST['rating']) && isset($_POST))
{
    //make variables safe to insert
  $rating = mysql_real_escape_string($_POST['rating']);
  $songid = mysql_real_escape_string($_POST['song_id']);

    //query to insert data into table
    $sql = "
        INSERT INTO wp_song_ratings
        SET
        songid = '$songid',
        rating = '$rating',
        userid = '$user_id'";
    $result = mysql_query($sql);
    if(!$result)
    {
        echo "Failed to insert record";
    }
    else
    {
        echo "Record inserted successfully";
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:04:39+00:00Added an answer on May 27, 2026 at 5:04 am

    Add a UNIQUE KEY for userID and songID. If you don’t want them to change their rating you shouldn’t allow it from the front-end, but still make the check on the backend. It will fail if a UNIQUE KEY is in place.

    ALTER TABLE `wp_song_ratings` ADD UNIQUE KEY `user_song_key` (`userID`, `songID`)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to jquery ! I am using the code below to insert
Without Enterprise Data library,I am using the below code to insert some thing to
I'm using the code below to bulk insert a 30000 rows (1000 rows at
I using below code to insert new sms in inbox, but when i click
I am trying to insert values into Oracle 10g database using the code below
Is it possible to insert MySQL functions using bindValue() ? My code below just
I have an application using zend_gdata and create contact with the code below. $doc
I'm using the code below to insert content into a page. I need to
i am using below code to change the the font type of text view.
Currently i'm using below code which works well. $(#topperAtBaseLevel:visible, #lowerAtBaseLevel:visible, #midAtBaseLevel).hide(); any optimised code?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.