从iOS应用程序的Post数组,通过PHP文件输入数据库中的值。

时间:2022-09-07 11:04:25

I need to add values in my Database table via PHP file but i have values in an dynamic array that can have one,two or more than three values, on clicking the Button in my iOS application i am sending the array via post using ASHIHTTPRequest but in the Database the value is not getting entered. The value i am getting in my Log NSLog(@"selected values are %@",self.arrayValue);

我在数据库表中需要添加值通过PHP文件但是我有值在一个动态数组,可以有一个,两个或三个以上值,单击按钮我iOS应用程序通过post发送数组使用ASHIHTTPRequest但在数据库中输入的值是没有得到。我在日志NSLog中获得的值(@“选择的值是%@”,self.arrayValue);

@property (nonatomic, retain) NSMutableArray *arrayValue;:

@property (nonatomic, retain) NSMutableArray *arrayValue;

selected values are (
    1,
    2
)

I am using this code :

我使用的代码是:

 request = [ASIFormDataRequest requestWithURL:url];
 [request setPostValue:Id forKey:@"userid"];
 [request setPostValue:self.arrayValue forKey:@"svalueid"];

 [request setCompletionBlock:^{



    NSString *response = [request responseString];
    response = [request responseString];
    NSLog(@"Server response: %@", response);
    // Response shows that value has been entered in DB but it shows zero in value of "svalueid" and userid get the desired value.

In my PHP code :

在我的PHP代码中:

<?php

// array for JSON response
$response = array();
//$svalueid=array();


// check for required fields
if (isset($_REQUEST['userid']) && isset($_REQUEST['svalueid'])) {
  $userid = $_REQUEST['userid'];
  $svalueid = explode(",",$_REQUEST['svalueid']);


include 'connect.php'; 

// connecting to db
$db = new DB_CONNECT();


            $result = mysql_query("SELECT userid, svalueid
            FROM users
            WHERE userid = '$userid'");


    if (mysql_num_rows($result) > 0) {


    $result = mysql_query("DELETE FROM uses WHERE userid = '$userid'");

      foreach($svalueid as $value){

$result = mysql_query("INSERT INTO users(id, userid, svalueid) VALUES('','$userid', '$value')");

                                      }

}
    else
    {
      foreach($svalueid as $value){

$result = mysql_query("INSERT INTO users(id, userid, svalueid) VALUES('','$userid', '$value')");

 }
    }



// check if row inserted or not
if ($result) {
    // successfully inserted into database
    $response["success"] = 1;
    $response["message"] = "successful.";

    // echoing JSON response
    echo json_encode($response);
} else {
    // failed to insert row
    $response["success"] = 0;
    $response["message"] = "An error occurred.";

    // echoing JSON response
    echo json_encode($response);
}



} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Field's missing";

    // echoing JSON response
    echo json_encode($response);
}
?>

But this get's value = 0 in the field of svalueid in my DB table I don't know PHP that much deep. Please can any one help me out how should i send my array values to the database tables.

但是在我的DB表中,在svalueid字段中得到的值= 0,我不知道PHP有多深。谁能帮我把数组的值发送到数据库表。

I also tried sending the data in json format but to able to implement it.

我还尝试了以json格式发送数据,但是能够实现它。

 NSMutableDictionary *jsonDict = [[NSMutableDictionary alloc] init];
    NSMutableDictionary *tagData = [[NSMutableDictionary alloc] init];
    for(int i = 0; i < arrayValue.count; i++)
    {
        NSString *keyString = [NSString stringWithFormat:@"%i", i];
        [tagData setObject:[arrayValue objectAtIndex:i] forKey:keyString];

    }

    [jsonDict setObject:tagData forKey:@"svalueid"];



    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDict options:NSJSONWritingPrettyPrinted error:nil];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

[request setPostValue:Id forKey:@"userid"];

[request setPostValue:jsonString forKey:@"svalueid"];

And Now on my console i am getting this value NSLog(@"json json string going on server is %@",jsonString);

现在在我的控制台上,我得到了这个值NSLog(@"json json字符串在服务器上是%@" jsonString);

 json string going on server is {
  "svalueid" : {
    "0" : 1,
    "1" : 2
  }
}

Please Now i don't know if it's correct or not and how should i modify my PHP File for it please i need help in this. Please if i did something wrong in my code can anyone correct it.

现在我不知道它是否正确,我该如何修改PHP文件,我需要帮助。如果我在代码中做错了什么,请大家纠正。

1 个解决方案

#1


1  

$svalueid is a json string with key=>value pairs so your foreach statements should look like this:

$svalueid是一个json字符串,键=>值对,所以foreach语句应该如下所示:

foreach($svalueid as $key=>$value)

instead of this:

而不是:

foreach($svalueid as $value)

The queries would also need to be updated to correctly use the $key and $value variables. I don't know what those numbers represent in your json string so I'm not sure how to give you a corrected query.

查询还需要更新以正确使用$key和$value变量。我不知道这些数字在json字符串中代表什么,所以我不知道如何给您一个正确的查询。

#1


1  

$svalueid is a json string with key=>value pairs so your foreach statements should look like this:

$svalueid是一个json字符串,键=>值对,所以foreach语句应该如下所示:

foreach($svalueid as $key=>$value)

instead of this:

而不是:

foreach($svalueid as $value)

The queries would also need to be updated to correctly use the $key and $value variables. I don't know what those numbers represent in your json string so I'm not sure how to give you a corrected query.

查询还需要更新以正确使用$key和$value变量。我不知道这些数字在json字符串中代表什么,所以我不知道如何给您一个正确的查询。