简体   繁体   English

如何检查是否存在包含MySQL中某些内容的行

[英]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. 我正在记录songID,对该歌曲的评级以及对该歌曲进行投票的用户ID。

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. 所以...如果userID = 1,songID = 1,等级=4。这应该可以插入。

If subsequently, an insertion attempt is made for userID=1, songID=1, rating=*, it should fail to insert 如果随后尝试插入userID = 1,songID = 1,rating = *,则插入失败

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 . userIDsongID添加一个UNIQUE KEY 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. 如果安装了UNIQUE KEY ,它将失败。

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: 您可以首先执行SELECT语句:

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子句添加到您的sql语句中

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 可以在Oracle和SQL Server上运行...对MySQL不确定

How about to use REPLACE query for it? 如何使用REPLACE查询呢? REPLACE query will replace the old one that has same primary key ( or unique key ) without an error. REPLACE查询将替换具有相同主键(或唯一键)且没有错误的旧查询。 I think some user may want to update their votes. 我认为某些用户可能想更新他们的投票。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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