php - 适用于 iOS 的简单 PHP/MySQL Web 服务

标签 php database ios json

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

我的数据库是这样的:id | rw_promo_code_id | email_id |设备编号 |赎回时间

我在 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

我怎样才能让这两个值(device_id 和 email_id)显示在数据库中,而不是只有一个?

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

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]: 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("iss", $id, $device_id, $email_id);

"is" 更改为 "iss" 或 "iis"所需的任何一项。您可以在

上获得有关 bind_param 的更多信息

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

关于php - 适用于 iOS 的简单 PHP/MySQL Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6156411/

相关文章:

javascript - Prestashop 自动完成

php - Symfony2 在使用 twig 呈现文本时使用 css 删除部分文本

mysql - 这个查询必须嵌套吗?

ios - iOS 11 Swift 下面的 UITabBar 转换问题

iOS7 QLPreviewController 更改导航栏颜色?

javascript - 将对象数组和嵌套数组转换为 JSON

php - 如何在不在客户端系统上安装 xampp 的情况下运行 php 应用程序?

android - 未创建sqlite数据库

mysql - 显示外键等于某个值的所有表

ios - 如何动态准备选择器以与respondsToSelector匹配