简体   繁体   中英

How to check if a row exists which contains certain content in MySQL

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";
    }
}

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`)

听起来您需要在mysql中使用存储过程-此处提供更多信息http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html

You can first do a SELECT statement:

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']);

  $select_sql = "SELECT COUNT(*) WHERE songid='$songid' AND userid='$user_id'";
  $select_result = mysql_query($select_sql);

  if ( mysql_num_rows($select_result) > 0 )
  {
    /* already voted! */
  }
  else
  {
    //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";
    }
  }
}

Of course, there are also other ways to implement such things (like stored procedures).

Add the following WHERE clause to your sql statement

WHERE NOT EXISTS (SELECT 1 FROM wp_song_ratings WHERE songid= '$songid' and userid = '$user_id')

That works on Oracle and SQL Server...not exactly sure about MySQL

How about to use REPLACE query for it? REPLACE query will replace the old one that has same primary key ( or unique key ) without an error. I think some user may want to update their votes.

我会更改加载方式的视图,方法是在当前用户已经为歌曲投票后不允许“投票”,或者如果您想让他们更改评分,请更新该内容,而不是在同一用户中对该用户进行插入歌曲..

如果已定义该表,使得(songid,userid)对于每个记录都必须是唯一的(通过将这对定义为键),则如果用户尝试对同一首歌曲进行重新投票,则插入将失败。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM