MYSQL如果存在,更新,否则插入。几乎可以,除了一件事

MYSQL如果存在,更新,否则插入。几乎可以,除了一件事,mysql,insert,Mysql,Insert,我正在做一个Flex/Php项目。我已经编写了这个php脚本,这样如果一个艺术家还不在数据库中,我就插入他,并将like_分数设为1。如果他已经存在,我们通过添加+1来更新like_分数。这就是我到目前为止所做的: // Variables $php_artist = $_POST["rma_artist"]; /* * A simple query to know if the artist exists in the DB by its name. We can't use id sin

我正在做一个Flex/Php项目。我已经编写了这个php脚本,这样如果一个艺术家还不在数据库中,我就插入他,并将like_分数设为1。如果他已经存在,我们通过添加+1来更新like_分数。这就是我到目前为止所做的:

// Variables
$php_artist = $_POST["rma_artist"];


/*
* A simple query to know if the artist exists in the DB by its name. We can't use id since we won't get that value.
* if $numrows > 0, we have a match. We calculate the number of likes and then, we update.
* else, we insert.
*/
$query = "SELECT like_score FROM rma_likelist WHERE like_artist = '$php_artist'";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
$numrows = mysql_num_rows($result);

// UPDATE if there is a row that matches, we update
if ($numrows > 0) {
$newLikeScore = $row["like_score"] + 1;
$query = "UPDATE rma_likelist SET like_score = '$newLikeScore' like_artist = $php_artist";

if ( !mysql_query($query, $mysql_connection) ){
    die('ERROR: '. mysql_error() );
}

// id, name, likes, operation
$response = $php_artist_id.":::".$php_artist.":::".$newLikeScore.":::MYSQL UPDATE SUCCESSFULL";
}
// INSERT if no rows match, we insert a new record
else {
$query = "INSERT INTO rma_likelist (like_artist, like_score) VALUES ('$php_artist', '1')";

$message = "INSERT SUCCESSFULL, 1 Record Added";

if ( !mysql_query($query, $mysql_connection) ){
    die('ERROR: '. mysql_error() );
}

$new_id = mysql_insert_id();

// id, name, likes, operation
$response = $new_id.":::".$php_artist.":::1:::MYSQL INSERT SUCCESSFULL";
}



echo $response;

?>
(或参见pastebin:)


它几乎可以工作:如果一个艺术家还不在数据库中,它会插入它并给它一个1分。它还可以识别数据库中是否已有艺术家,因为它不会复制任何艺术家。但是,它永远不会更新数据库中已经存在的乐队的like_分数,它始终保持为1。为什么会这样?我一直在寻找一个很长的时间=/

你应该使用
MERGE
来做你想做的事情:

MERGE into rma_likelist using (select pk_artist_id from rma_likelist where pk_artist_id = theid)
when not matched then insert into rma_likelist values (...)
when matched then update rma_likelist set like_score = like_score + 1 where ...
(查询不完整是否您的脚本中有我不理解的部分)


这里的明显优势是,您甚至不需要查询来检查艺术家的存在——减少了一个bug源;)

我认为喜欢的分数将在$row[0][“喜欢的分数”]

您缺少更新查询中的位置 $query=“更新rma\U likelist集合like\U分数='$NEWLICESCORE'其中like\U艺术家=$php\U艺术家”


在这些情况下,最好的做法是使用mysql的功能使like_成为唯一的。由于UPDATE语句不完整,您的代码可能无法工作:

$query = "UPDATE rma_likelist SET like_score = '$newLikeScore' 
  /* missing WHERE keyword */ like_artist = $php_artist";
该语句应包括
。。。其中like_artist=…

我原以为您的错误会导致语法错误,我看到您正在检查来自
mysql\u query()
的错误结果,所以我不确定您为什么没有检测到它

另外,如果
$php\u artist
是一个字符串,则在UPDATE语句中没有引用它

您还应小心创建漏洞


也就是说,我建议您可以通过使用


PS:如上图所示,我还将使用带有查询参数的PDO,而不是过时的ext/mysql。

我想你指的是$response?这是为了将信息返回到我的Flex项目。但是错误肯定不在那一行,因为我可以检查数据库。我会尝试合并,看看是否有效。谢谢.fwiw,MySQL不支持标准SQL
MERGE
语句。是的,艺术家名称是唯一的。我不能使用唯一的id,因为我从api获取艺术家名称(也就是说,我不获取id,只获取艺术家名称)。
$query = "INSERT INTO rma_likelist (like_artist, like_score) VALUES (?, 1)
    ON DUPLICATE KEY UPDATE like_score = like_score + 1";

$stmt = $pdo->prepare($query);

$stmt->execute( array($php_artist) );