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 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真