0

Based on : http://www.raywenderlich.com/2941/how-to-write-a-simple-phpmysql-web-service-for-an-ios-app

My database looks like this : id | rw_promo_code_id | email_id | device_id | redeemed_time

I try this in the index.php:

// Check for required parameters
            if (isset($_POST["rw_app_id"]) && isset($_POST["code"]) && isset($_POST["email_id"]) && isset($_POST["device_id"]) ) {

...........    
                // Add tracking of redemption
                $stmt = $this->db->prepare("INSERT INTO rw_promo_code_redeemed (rw_promo_code_id, email_id, device_id) VALUES (?, ?, ?)");
                $stmt->bind_param("is", $id, $email_id, device_id);
                $stmt->execute();
                $stmt->close();

IF I REMOVE && isset($_POST["device_id"]) and make this line

$stmt = $this->db->prepare("INSERT INTO rw_promo_code_redeemed 
                      (rw_promo_code_id, email_id, device_id) VALUES (?, ?, ?)");

to

$stmt = $this->db->prepare("INSERT INTO rw_promo_code_redeemed
               (rw_promo_code_id, email_id) VALUES (?, ?)");

I GET THE EMAIL CORRECTLY IN THE DATABASE OF COURSE, BUT NOT THE DEVICE ID

How can I get the the both values (device_id and email_id) to display in the database and not only one ?

I use this in the app (there is no problem with this)

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];

EDIT : HERE IS THE ORGINAL FUNCTION:

function redeem() {

    // Check for required parameters
    if (isset($_POST["rw_app_id"]) && isset($_POST["code"]) && 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->db->prepare('SELECT id, unlock_code, uses_remaining FROM rw_promo_code WHERE rw_app_id=? AND code=?');
        $stmt->bind_param("is", $rw_app_id, $code);
        $stmt->execute();
        $stmt->bind_result($id, $unlock_code, $uses_remaining);
        while ($stmt->fetch()) {
            break;
        }
        $stmt->close();

        // Bail if code doesn't exist
        if ($id <= 0) {
            sendResponse(400, 'Invalid code');
            return false;
        }

        // Bail if code already used        
        if ($uses_remaining <= 0) {
            sendResponse(403, 'Code already used');
            return false;
        }   

        // Check to see if this device already redeemed 
        $stmt = $this->db->prepare('SELECT id FROM rw_promo_code_redeemed WHERE device_id=? AND rw_promo_code_id=?');
        $stmt->bind_param("si", $device_id, $id);
        $stmt->execute();
        $stmt->bind_result($redeemed_id);
        while ($stmt->fetch()) {
            break;
        }
        $stmt->close();

        // Bail if code already redeemed
        if ($redeemed_id > 0) {
            sendResponse(403, 'Code already used');
            return false;
        }

        // Add tracking of redemption
        $stmt = $this->db->prepare("INSERT INTO rw_promo_code_redeemed (rw_promo_code_id, device_id) VALUES (?, ?)");
        $stmt->bind_param("is", $id, $device_id);
        $stmt->execute();
        $stmt->close();

        // Decrement use of code
        $this->db->query("UPDATE rw_promo_code SET uses_remaining=uses_remaining-1 WHERE id=$id");
        $this->db->commit();

        // Return unlock code, encoded with JSON
        $result = array(
            "unlock_code" => $unlock_code,
        );
        sendResponse(200, json_encode($result));
        return true;
    }
    sendResponse(400, 'Invalid request');
    return false;

}

EDIT: The error

PHP Warning:  mysqli_stmt::bind_param() [<a

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

which is :

$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);

9
  • what is the error message when you add && isset($_POST["device_id"]) Commented May 27, 2011 at 18:46
  • I don't see any error message .. ? how could I get it ?? Commented May 27, 2011 at 18:48
  • what happens when you add ** isset($_POST..)** does it runs Commented May 27, 2011 at 18:52
  • Nothing.. it always send me the 200 response but it doesn't write the data in the database Commented May 27, 2011 at 19:03
  • so i guess you not posting values for device_id... check post data either from firebug or simple add var_dump($_POST) to your PHP code Commented May 27, 2011 at 19:05

1 Answer 1

4

got you...

you are missing value type for third variable.

$stmt = $this->db->prepare("INSERT INTO rw_promo_code_redeemed (rw_promo_code_id, device_id, email_id) VALUES (?, ?, ?)"); $stmt->bind_param("iss", $id, $device_id, $email_id);

change "is" to "iss" or "iis" any one required. you can get further info about bind_param on

http://www.php.net/manual/en/mysqli-stmt.bind-param.php

Sign up to request clarification or add additional context in comments.

1 Comment

OMG, what a stupid error... Thanks for the help.. I feel dumb .. THANKS AGAIN

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.