Post Array from iOS app to enter values in database via PHP File -
i need add values in database table via php file have values in dynamic array can have one,two or more 3 values, on clicking button in ios application sending array via post using ashihttprequest in database value not getting entered. value getting in log nslog(@"selected values %@",self.arrayvalue);
@property (nonatomic, retain) nsmutablearray *arrayvalue;:
selected values ( 1, 2 ) i using 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 value has been entered in db shows 0 in value of "svalueid" , userid desired value. in php code :
<?php // array json response $response = array(); //$svalueid=array(); // check required fields if (isset($_request['userid']) && isset($_request['svalueid'])) { $userid = $_request['userid']; $svalueid = explode(",",$_request['svalueid']); include 'connect.php'; // connecting db $db = new db_connect(); $result = mysql_query("select userid, svalueid users userid = '$userid'"); if (mysql_num_rows($result) > 0) { $result = mysql_query("delete uses userid = '$userid'"); foreach($svalueid $value){ $result = mysql_query("insert users(id, userid, svalueid) values('','$userid', '$value')"); } } else { foreach($svalueid $value){ $result = mysql_query("insert users(id, userid, svalueid) values('','$userid', '$value')"); } } // check if row inserted or not if ($result) { // inserted database $response["success"] = 1; $response["message"] = "successful."; // echoing json response echo json_encode($response); } else { // failed insert row $response["success"] = 0; $response["message"] = "an error occurred."; // echoing json response echo json_encode($response); } } else { // required field missing $response["success"] = 0; $response["message"] = "field's missing"; // echoing json response echo json_encode($response); } ?> but get's value = 0 in field of svalueid in db table don't know php deep. please can 1 me out how should send array values database tables.
i tried sending data in json format able implement it.
nsmutabledictionary *jsondict = [[nsmutabledictionary alloc] init]; nsmutabledictionary *tagdata = [[nsmutabledictionary alloc] init]; for(int = 0; < 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 on console getting value nslog(@"json json string going on server %@",jsonstring);
json string going on server { "svalueid" : { "0" : 1, "1" : 2 } } please don't know if it's correct or not , how should modify php file please need in this. please if did wrong in code can correct it.
$svalueid json string key=>value pairs foreach statements should this:
foreach($svalueid $key=>$value) instead of this:
foreach($svalueid $value) the queries need updated correctly use $key , $value variables. don't know numbers represent in json string i'm not sure how give corrected query.
Comments
Post a Comment