【问题标题】:A Simple PHP/MySQL Web Service for iOS适用于 iOS 的简单 PHP/MySQL Web 服务
【发布时间】:2011-09-03 15:01:44
【问题描述】:

基于: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 |赎回时间

我在 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)同时显示在数据库中,而不仅仅是一个?

我在app里用这个(这个没问题)

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 上 第 134 行

这是:

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

【问题讨论】:

  • 添加&& isset($_POST["device_id"])时的错误信息是什么
  • 我没有看到任何错误信息 .. ?我怎么能得到它??
  • 当你添加 ** isset($_POST..)** 时会发生什么,它会运行吗
  • 没什么.. 它总是向我发送 200 响应,但它不会将数据写入数据库
  • 所以我猜你没有发布 device_id 的值...检查来自 firebug 的发布数据或简单地将 var_dump($_POST) 添加到你的 PHP 代码

标签: php database ios json


【解决方案1】:

让你...

缺少第三个变量的值类型

$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

【讨论】:

  • 天啊,多么愚蠢的错误...感谢您的帮助..我觉得很愚蠢..再次感谢
猜你喜欢
  • 1970-01-01
  • 2014-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多