duanbarong4617 2013-08-23 01:38
浏览 52
已采纳

这个PHP代码有效吗?

I'm currently working on an iOS app, which communicates with a database on a server (REST API). I've managed to send simple post queries to the server and successfully getting responses, but since a few hours I am stuck with the following php-function (it was not written by myself!). My questions:

  1. Is this valid php-Code?

  2. Is it possible to POST both JSON-Objects and non-JSON Objects at the same time?

  3. How would a valid request query look like? (I am using the Google Chrome App "Postman - REST Client" to test the queries) So what would the parameters look like if a wanted to pass tableid = 1, clientid = 1 and json = {1,2,3,4}?

Thank you very much!

if($_POST['function'] == 'addOrder'){

    $sql = "INSERT INTO orders SET
                orderdate = NOW(),
                tableid = '".$_POST['tableid']."',
                clientid = '".$_POST['clientid']."'";
    $result = mysql_query($sql);
    $oid = mysql_insert_id();

    $orderitems = json_decode($_POST['json'],true);

    reset($orderitems);
    while(list(,$oitem) = each($orderitems)){
        $sql = "INSERT INTO orderitems SET
                        orderid = '".$oid."',
                        foodid = '".$oitem['id']."";    
        $result = mysql_query($sql);
    }
}
  • 写回答

1条回答 默认 最新

  • du532861657 2013-08-24 03:19
    关注

    You ask:

    Is it possible to POST both JSON-Objects and non-JSON Objects at the same time?

    How would a valid request query look like? (I am using the Google Chrome App "Postman - REST Client" to test the queries) So what would the parameters look like if a wanted to pass tableid = 1, clientid = 1 and json = {1,2,3,4}?

    Yes, you can post both JSON and non-JSON in a single request. The JSON {1,2,3} doesn't make sense, though. If it is a simple array, that would be [1,2,3]:

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    NSString *messageBody = @"function=addOrder&tableid=1&clientid=2&json=[1,2,3]";
    NSData   *messageData = [messageBody dataUsingEncoding:NSUTF8StringEncoding];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:messageData];
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        if (error)
            NSLog(@"sendAsynchronousRequest error: %@", error);
        if (data) {
            NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"%@", string);
        }
    }];
    

    Note, in your while loop, you refer to $oitem['id'], but I don't understand this id you're referring to. Given that $orderitems is a simple array, each item is referenced by $oitem alone, (no ['id']).

    And, as an aside, I generally use the foreach syntax:

    foreach ($orderitems as $oitem) {
        // now I can refer to $oitem
    }
    

    If your JSON was [{"id":1},{"id":2},{"id":3}], then you'd refer to $oitem['id'], but if it's [1,2,3], you just refer to $oitem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办