Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Dec 17th, 2011  |  syntax: PHP  |  size: 1.60 KB  |  views: 26  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // Variables
  2.  
  3. $php_artist = $_POST["rma_artist"];
  4.  
  5.  
  6.  
  7.  
  8.  
  9. /*
  10.  
  11. * a simple query to know if the artist exists in the DB by its name, since we won't get the id value.
  12.  
  13. * if $numrows > 0, we have a match. We calculate the number of likes and then, we update.
  14.  
  15. * else, we insert.
  16.  
  17. */
  18.  
  19. $query = "SELECT like_score FROM rma_likelist WHERE like_artist = '$php_artist'";
  20.  
  21. $result = mysql_query($query);
  22.  
  23. $row = mysql_fetch_assoc($result);
  24.  
  25. $numrows = mysql_num_rows($result);
  26.  
  27.        
  28.  
  29. // UPDATE if there is a row that matches, we update
  30.  
  31. if ($numrows > 0) {
  32.  
  33.         $newLikeScore = $row["like_score"] + 1;
  34.  
  35.         $query = "UPDATE rma_likelist SET like_score = '$newLikeScore' WHERE like_artist = $php_artist";
  36.  
  37.        
  38.  
  39.         if ( !mysql_query($query, $mysql_connection) ){
  40.  
  41.             die('ERROR: '. mysql_error() );
  42.  
  43.         }
  44.  
  45.        
  46.  
  47.         // id, name, likes, operation
  48.  
  49.         $response = $php_artist_id.":::".$php_artist.":::".$newLikeScore.":::MYSQL UPDATE SUCCESSFULL";
  50.  
  51. }
  52.  
  53. // INSERT if no rows match, we insert a new record
  54.  
  55. else {
  56.  
  57.         $query = "INSERT INTO rma_likelist (like_artist, like_score) VALUES ('$php_artist', '1')";
  58.  
  59.        
  60.  
  61.         $message = "INSERT SUCCESSFULL, 1 Record Added";
  62.  
  63.        
  64.  
  65.         if ( !mysql_query($query, $mysql_connection) ){
  66.  
  67.             die('ERROR: '. mysql_error() );
  68.  
  69.         }
  70.  
  71.        
  72.  
  73.         $new_id = mysql_insert_id();
  74.  
  75.        
  76.  
  77.         // id, name, likes, operation
  78.  
  79.         $response = $new_id.":::".$php_artist.":::1:::MYSQL INSERT SUCCESSFULL";
  80.  
  81. }
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89. echo $response;
  90.  
  91.  
  92.  
  93. ?>