I'm working on a Flex/Php project. I have written this php script so that if an artist is not in the database yet, i insert him and put the like_score to 1. If he does already exists, we update the like_score by adding +1. This is what i have so far:
// 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;
?>
(or see pastebin: http://pastebin.com/HwHYXaGP)
It almost works: If an artist isn't in the database yet, it will insert it and give it a like_score of 1. It also recognizes if an artist is already in the database, because it will not duplicate any artists. However, it will never update the like_score of a band that is already in the database, it always stays 1. Why does that happen? I've been looking for a looong time =/