当我输入符号“&”时,我通过表单有一个带有SLIM3的简单API。我的数据库中填充了一个空行。缺少符号“&”有效。
良好JSON的示例->
Object { post_ID: 0, post_date: "2018-09-29", post_content: "Test post contest", post_image: "image-1538221739-psql-324x235.png", article_tittle: "Test article tittle", article_content: "<ol><li><strong>Test Body</strong></li><li>Test 2</li></ol>", post_category: "DEV" }
createpost.component.ts:34:2
Object { status: "success", code: "200", message: "FIle uploaded.", filename: "image-1538221748-psql-324x235.png" }
错误JSON的示例->
Object { post_ID: 0, post_date: "2018-09-29", post_content: "Test post contest", post_image: "image-1538221748-psql-324x235.png", article_tittle: "Test article tittle&", article_content: "<ol><li><strong>Test Body</strong></li><li>ó ñ</li></ol>", post_category: "DEV" }
createpost.component.ts:34:2
Object { status: "success", code: "200", message: "FIle uploaded.", filename: "image-1538221829-psql-324x235.png" }
如您所见,第一个JSON在数据库中正确填充,但第二个JSON由于符号“&”填充空行。
这是我创建帖子的功能。
//Create post
$app->post('/posts/', function() use($app, $db){
$json = $app->request->post('json');
$data =json_decode($json, true);
if (!isset($data['post_date'])){
$data['post_date']=null;
}
if (!isset($data['post_content'])){
$data['post_content']=null;
}
if (!isset($data['post_category'])){
$data['post_category']=null;
}
if (!isset($data['post_image'])){
$data['post_image']=null;
}
if (!isset($data['article_tittle'])){
$data['article_tittle']=null;
}
if (!isset($data['article_content'])){
$data['article_content']=null;
}
$stmt = $db->prepare("INSERT INTO posts ( post_ID, post_date, post_content, post_category, post_image, article_tittle, article_content) VALUES ( NULL, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("ssssss", $post_date, $post_content, $post_category, $post_image, $article_tittle, $article_content);
$post_date = $data['post_date'];
$post_content = $data['post_content'];
$post_category = $data['post_category'];
$post_image = $data['post_image'];
$article_tittle = $data['article_tittle'];
$article_content = $data['article_content'];
$stmt->execute();
//$query = "INSERT INTO posts VALUES(NULL,
//"."'{$data['post_date']}'".",
//"."'{$data['post_content']}'".",
//"."'{$data['post_category']}'".",
//"."'{$data['post_image']}'".",
//"."'{$data['article_tittle']}'".",
//"."'{$data['article_content']}'".")";
//$insert = $db->query($query);
$result = array(
'status' => 'error',
'code' => '404',
'message' => 'Post not added.'
);
if($stmt){
$result = array(
'status' => 'success',
'code' => '200',
'message' => 'Post added.'
);
}
echo json_encode($result);
$stmt->close();
});
我尝试使用http://php.net/manual/es/function.json-encode.php进行解码,但结果出乎意料。我在做什么错了?
感谢您的时间