// Variables $php_artist = $_POST["rma_artist"]; /* * a simple query to know if the artist exists in the DB by its name, since we won't get the id 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' WHERE 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; ?>