我正在尝试创建一个用于在数据库中插入数据的iOS应用程序,我在一个url中传递了4个数组,使用JSON在数据库中插入值并将其发送到服务器端但在php文件中但是我收到了这样的错误 -
"Error Domain=NSCocoaErrorDomain Code=3840 "Garbage at end." UserInfo={NSDebugDescription=Garbage at end".
但是当我试图从浏览器运行该php文件时,我就成功插入了消息。
这是我的应用程序端代码
NSString *url_string = [NSString
stringWithFormat:@"itemid[]=%@&itemname[]=%@&ReqQty[]=%@& ItmUnits[]=%@&SiteFrom=%@&SiteTo=%@& ReqDate=%@",_itemcode,_itemname,_ReqQty,_ItmUnits,_site_from.text,_site_to .text,_date.text];;
NSLog(@"%@",url_string);
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config delegate:nil delegateQueue:[NSOperationQueue mainQueue]];
NSURL *url = [NSURL URLWithString:@"http://anantsoftcomputing.com/ConstructSkills/require_test.php?format=json"];
NSMutableURLRequest *urlrequest = [NSMutableURLRequest requestWithURL:url];
[urlrequest setHTTPMethod:@"POST"];
[urlrequest setHTTPBody:[url_string dataUsingEncoding:NSUTF8StringEncoding]];
// NSLog(@"%@",urlrequest);
NSURLSessionDataTask *datatask = [session dataTaskWithRequest:urlrequest completionHandler:^(NSData *dataRaw, NSURLResponse *response, NSError *error)
{
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:dataRaw options:NSJSONReadingAllowFragments error:&error];
// NSString *status = json[@"status"];
NSLog(@"%@",error);
NSLog(@"count : %@",[json description]);
}];
[datatask resume];
这是我的PHP代码
<?php
ini_set("display_errors","on");
include 'db_connect.php';
//request master
$req = "INSERT INTO requirement_master (req_id) values (null)";
$res = mysqli_query($con,$req);
//echo $res;
$reqid = mysqli_insert_id($con);
//singal variables
$sitefrom = $_REQUEST['SiteFrom'];
$siteto = $_REQUEST['SiteTo'];
$reqdate = $_REQUEST['ReqDate'];
// array variables // will get values from url as array format
$idarray = implode(",", $_REQUEST['itemid']);
$namearray = implode(",", $_REQUEST['itemname']);
$reqarray = implode(",", $_REQUEST['ReqQty']);
$unitarray = implode(",", $_REQUEST['ItmUnits']);
//remove %20
//will remove square bracets from your string return by implode
$x =trim($idarray,'()');
$a = str_replace(' ','',$x);
$b =trim($namearray,'()');
$c =trim($reqarray, '()');
$d =trim($unitarray, '()');
//$e =trim($rcvqtyarray, '()');
//$f =trim($qtydiffarray, '()');
//will convert your array values into string
$itemid = explode(",",$a);
$itemname = explode(",",$b);
$reqqty = explode(",",$c);
$itemunits = explode(",",$d);
$c = count($itemid);
// start inserting values to respective table
for($i=0; $i< $c; $i++)
{
$query = "INSERT INTO requirement_form (Reqitem_id,req_id_master,item_name,quantity_req,units,site_from,site_to,date) VALUES
('$itemid[$i]','$reqid','$itemname[$i]','$reqqty[$i]','$itemunits[$i]','$sitefrom','$siteto','$reqdate')" ;
$result = mysqli_query($con,$query);
echo $result;
if($result)
{
$flag['code']= "Successfully registered";
$flag['id'] = $reqid;
}
else
{
// $flag['code']= "Database query failed";
$flag['code']= mysqli_error($con);
}
echo json_encode($flag);
}
?>