一个简单的PHP / MySQL Web服务的iOS [英] A Simple PHP/MySQL Web Service for iOS

查看:123
本文介绍了一个简单的PHP / MySQL Web服务的iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我的数据库如下所示:id | rw_promo_code_id | email_id | device_id | redeemed_time

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

我在index.php中尝试:

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

如果我移除&& isset($ _ POST [device_id])并创建此行

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

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

我在电子邮件数据库中正确获取电子邮件,但不是设备ID

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

如何获取这两个值(device_id和email_id)显示在数据库中,而不只是一个?

我在应用程序中使用这个(这没有问题)

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

编辑:这是原始功能:

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;

}

编辑:错误


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

href ='mysqli-stmt.bind-param'> mysqli-stmt.bind-param]:
类型定义中的元素数
字符串不匹配绑定数
变量
/Applications/MAMP/htdocs/index.php on
line 134

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

这是:


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

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


推荐答案

让你...

你是第三个变量的缺失值类型 >

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

更改为iss或iis任何一个。您可以在

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

这篇关于一个简单的PHP / MySQL Web服务的iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆