2

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);

@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

// 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.

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

 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);

 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.

3
  • 1
    Have you verified that you are sending what you think you are? Create a httpbin and post to it. requestb.in Commented Aug 16, 2013 at 17:31
  • In your first attempt I'm not seeing where it's clear that you were sending the string value you might have thought you were sending. It might be that you just need to use NSArray's componentsJoinedByString: method to get the string you want to send over. Commented Aug 16, 2013 at 19:15
  • I am sending the value in jsonString ... and on console it seems to be ok ... i think i need to work out on the PHP file ... can you please help me out in that ? Commented Aug 19, 2013 at 11:26

1 Answer 1

1

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

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.