title: A Simple PHP/MySQL Web Service for iOS
body: <p>Based on : <a href="http://www.raywenderlich.com/2941/how-to-write-a-simple-phpmysql-web-service-for-an-ios-app" rel="nofollow">http://www.raywenderlich.com/2941/how-to-write-a-simple-phpmysql-web-service-for-an-ios-app</a></p>
  
  <p>My database looks like this : id | rw_promo_code_id |   email_id  | device_id | redeemed_time</p>
  
  <p>I try this in the index.php: </p>
  
  <pre><code>// Check for required parameters
              if (isset($_POST["rw_app_id"]) &amp;&amp; isset($_POST["code"]) &amp;&amp; isset($_POST["email_id"]) &amp;&amp; isset($_POST["device_id"]) ) {
  
  ...........    
                  // Add tracking of redemption
                  $stmt = $this-&gt;db-&gt;prepare("INSERT INTO rw_promo_code_redeemed (rw_promo_code_id, email_id, device_id) VALUES (?, ?, ?)");
                  $stmt-&gt;bind_param("is", $id, $email_id, device_id);
                  $stmt-&gt;execute();
                  $stmt-&gt;close();
  </code></pre>
  
  <p>IF I REMOVE <code>&amp;&amp; isset($_POST["device_id"])</code> and make this line </p>
  
  <pre><code>$stmt = $this-&gt;db-&gt;prepare("INSERT INTO rw_promo_code_redeemed 
                        (rw_promo_code_id, email_id, device_id) VALUES (?, ?, ?)");
  </code></pre>
  
  <p>to </p>
  
  <pre><code>$stmt = $this-&gt;db-&gt;prepare("INSERT INTO rw_promo_code_redeemed
                 (rw_promo_code_id, email_id) VALUES (?, ?)");
  </code></pre>
  
  <p>I GET THE EMAIL CORRECTLY IN THE DATABASE OF COURSE, BUT NOT THE DEVICE ID</p>
  
  <p><strong>How can I get the the both values (device_id and email_id) to display in the database and not only one ?</strong></p>
  
  <p>I use this in the app (there is no problem with this)</p>
  
  <pre><code>NSString *emailadrress = email.text;
      NSURL *url = [NSURL URLWithString:@"http://localhost:8888/"];
      ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
      [request setPostValue:@"1" forKey:@"rw_app_id"];
      [request setPostValue:code forKey:@"code"];
      [request setPostValue:emailadrress  forKey:@"email_id"];
      [request setPostValue:@"23131" forKey:@"device_id"];
      [request setDelegate:self];
      [request startAsynchronous];
  </code></pre>
  
  <p>EDIT : HERE IS THE ORGINAL FUNCTION:</p>
  
  <pre><code>function redeem() {
  
      // Check for required parameters
      if (isset($_POST["rw_app_id"]) &amp;&amp; isset($_POST["code"]) &amp;&amp; isset($_POST["device_id"])) {
  
          // Put parameters into local variables
          $rw_app_id = $_POST["rw_app_id"];
          $code = $_POST["code"];
          $device_id = $_POST["device_id"];
  
          // Look up code in database
          $user_id = 0;
          $stmt = $this-&gt;db-&gt;prepare('SELECT id, unlock_code, uses_remaining FROM rw_promo_code WHERE rw_app_id=? AND code=?');
          $stmt-&gt;bind_param("is", $rw_app_id, $code);
          $stmt-&gt;execute();
          $stmt-&gt;bind_result($id, $unlock_code, $uses_remaining);
          while ($stmt-&gt;fetch()) {
              break;
          }
          $stmt-&gt;close();
  
          // Bail if code doesn't exist
          if ($id &lt;= 0) {
              sendResponse(400, 'Invalid code');
              return false;
          }
  
          // Bail if code already used        
          if ($uses_remaining &lt;= 0) {
              sendResponse(403, 'Code already used');
              return false;
          }   
  
          // Check to see if this device already redeemed 
          $stmt = $this-&gt;db-&gt;prepare('SELECT id FROM rw_promo_code_redeemed WHERE device_id=? AND rw_promo_code_id=?');
          $stmt-&gt;bind_param("si", $device_id, $id);
          $stmt-&gt;execute();
          $stmt-&gt;bind_result($redeemed_id);
          while ($stmt-&gt;fetch()) {
              break;
          }
          $stmt-&gt;close();
  
          // Bail if code already redeemed
          if ($redeemed_id &gt; 0) {
              sendResponse(403, 'Code already used');
              return false;
          }
  
          // Add tracking of redemption
          $stmt = $this-&gt;db-&gt;prepare("INSERT INTO rw_promo_code_redeemed (rw_promo_code_id, device_id) VALUES (?, ?)");
          $stmt-&gt;bind_param("is", $id, $device_id);
          $stmt-&gt;execute();
          $stmt-&gt;close();
  
          // Decrement use of code
          $this-&gt;db-&gt;query("UPDATE rw_promo_code SET uses_remaining=uses_remaining-1 WHERE id=$id");
          $this-&gt;db-&gt;commit();
  
          // Return unlock code, encoded with JSON
          $result = array(
              "unlock_code" =&gt; $unlock_code,
          );
          sendResponse(200, json_encode($result));
          return true;
      }
      sendResponse(400, 'Invalid request');
      return false;
  
  }
  </code></pre>
  
  <p>EDIT: The error </p>
  
  <blockquote>
  <pre><code>PHP Warning:  mysqli_stmt::bind_param() [&lt;a
  </code></pre>
    
    <p>href='mysqli-stmt.bind-param'>mysqli-stmt.bind-param]:
    Number of elements in type definition
    string doesn't match number of bind
    variables in
    /Applications/MAMP/htdocs/index.php on
    line 134</p>
  </blockquote>
  
  <p>which is :</p>
  
  <blockquote>
    <p>$stmt = $this->db->prepare("INSERT
    INTO rw_promo_code_redeemed
    (rw_promo_code_id, device_id,
    email_id) VALUES (?, ?, ?)");
                    $stmt->bind_param("is", $id,
    $device_id, $email_id);</p>
  </blockquote>