title: PHP to Update MySQL Database Table
body: <p>First off, just wanted to say I'm a novice at this type of coding, although I'm hopeful that I'll eventually make sense of it all with a little guidance.</p>
  
  <p>I have a MySQL database table (promotion) that stores a bunch of redemption codes for various products (for a give away contest). The idea is, the first person to enter the redemption code wins the product, and their info should be stored in the "promotion" table.</p>
  
  <p>The table's columns are: redeem_id (Auto Increment field), redeem_code, redeemer_email, redeemer_first_name, redeemer_last_name, and redeem_date_time.</p>
  
  <p>Initially, the redeem_id and redeem_code fields are the only ones with any data. What I'd like to happen is when a user enters their information (name, email, etc) and submit a redemption code, their info will populate the rest of the row for that particular code. If anyone else tries to submit a code that has already been redeemed, they should receive an error message - likewise for an invalid code (i.e. a code that does not exist in the table).</p>
  
  <p>The PHP code I have so far is:</p>
  
  <pre><code>&lt;?php
  function get_promotion_by_redeem_code($redeem_code)
  {
  $sql = "SELECT * FROM promotion WHERE redeem_code= '".mysql_real_escape_string($redeem_code)."'";
  $result = mysql_query($sql);
  $row = mysql_fetch_assoc($result);
  
  return $row;
  }
  
  function redeem_promotion($email,$first_name,$last_name,$redeem_date_time,$redeem_code)
  {
      $query = 'UPDATE promotion
      SET redeemer_email=".mysql_real_escape_string($email).", redeemer_first_name=".mysql_real_escape_string($first_name).", redeemer_last_name=".mysql_real_escape_string($last_name).", redeem_date_time=NOW(), WHERE redeem_code=".mysql_real_escape_string($redeem_code)."';
      $insert = mysql_query($query);
      return $insert;
  }
  
  $email=$_POST['e_mail'];
  $first_name=$_POST['f_name'];
  $last_name=$_POST['l_name'];
  $redeem_code=$_POST['v_code'];
  
  $connection = mysql_connect('localhost', 'db', 'pw');
  mysql_select_db('db', $connection);
  
  $promotion = get_promotion_by_redeem_code($redeem_code);
  
  if ($promotion) {
      if (!$promotion['redeemer_email']) {
          redeem_promotion($email,$first_name,$last_name,$redeem_date_time,$redeem_code);
          echo 'Congratulations, you have successfully claimed this item!';
      } else {
          echo 'Sorry, this item has already been redeemed.';
      }
  } else {
      echo 'Sorry, you have entered an incorrect claim code. Please use your browser\'s back button to try again.';
  }
  
  mysql_close($connection);  
  ?&gt;
  </code></pre>
  
  <p>It works as expected when I enter an invalid claim code, or if a code's row has been previously populated.
  When it doesn't work, is when someone goes to redeem the item for the first time. Essentially, it will show the "Congratulations" message, however the table doesn't get updated for the submitted information. Therefore, no matter how many times the correct code is entered, the user will receive the "Congratulations" message. </p>
  
  <p>I'm fairly certain that the error is in the redeem_promotion() function, but I can't figure out where.</p>