[英]how to send json format as input and store in database in codeigniter rest for webservices
am using the following code for sending json format as input and to store it in db. 我正在使用以下代码来发送json格式作为输入并将其存储在db中。 when i decode the value am getting nothing.
当我解码该值一无所获。 what was wrong in my code.
我的代码有什么问题。 can someone help me please.
有人能帮助我吗。
controller 控制者
function join_community_post(){
$serviceName = 'join_community';
//getting posted values
$c = '{"community" : [{"community_id":1},{"community_id":2},{"community_id":3}]}';
$ip = trim($this->input->post($c));
$ipJson = json_decode($ip);
print_r ($ipJson);exit;
$retVals = $this->user_model->user_join_community($ip, $serviceName);
header("content-type: application/json");
echo $retVals;
exit;
}
model 模型
function user_join_community($ip,$serviceName) {
$ipJson = json_decode($ip);
}
The json_decode gives the stdClass Object if second parameter is not passed as true PHP:json_decode 如果第二个参数未作为真正的PHP传递,则json_decode提供stdClass对象:
And second you are passing the post value of $c...check if its blank Input Class 其次,您正在传递$ c的后值...检查其空白输入类
Controller 控制者
function join_community_post(){
$serviceName = 'join_community';
//getting posted values
//$c = '{"community" : [{"community_id":1},{"community_id":2},{"community_id":3}]}';
$ip = trim($this->input->post($c));
$ipJson = json_encode($ip);
$retVals = $this->user_model->user_join_community($ip, $serviceName);
header("content-type: application/json");
echo $retVals;
exit;
}
Model 模型
function user_join_community($c,$serviceName) {
$ipJson = json_decode($c,true);
$createArray = array();
foreach($ipJson['community'] as $key=>$value){
$createArray = array(
'user_community_created_date' => date('Y-m-d H:i:s'),
'user_community_modified_date' => date('Y-m-d H:i:s'),
'community_id' => $value['community_id']
);
$insert = $this->db->insert('user_community', $createArray);
}
if ($insert) {
$data['message'] = 'success';
$status = $this->ville_lib->return_status('success', $serviceName, $data, $c);
} else {
$data['message'] = 'Error';
$status = $this->ville_lib->return_status('error', $serviceName, $data, $c);
}
}
Removed semicolon after curly bracket of foreach and added the $c to the webservices table which was getting you the error.....hope it helps... 在foreach的花括号后删除分号,并将$ c添加到webservices表中,这将使您收到错误.....希望它可以帮助...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.