我正在尝试将数据插入到表格
的json中 [{"name":"name", "username": "xyz", "email":"xyz@gmail.com", "password":"password0387"}]
进入同一文件夹中的文件。使用post方法传递数据,php将数据插入到json文件中。这是我修改过的php代码:
<?php
//checking if the script received a post request or not
if($_SERVER['REQUEST_METHOD']=='POST'){
//Getting post data
$name = $_POST['name'];
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
//checking if the received values are blank
if($name == '' || $username == '' || $password == '' || $email == ''){
//giving a message to fill all values if the values are blank
echo 'please fill all values';
}else{
//If the values are not blank
//Load the file
$contents = file_get_contents('user_doc.json');
//Decode the JSON data into a PHP array.
$json = json_decode($contents, true);
$user = array_search($username, array_column( $json, 'username' ) );
if( $user !== False )
$json[$user] = array("name" => $name, "username" => $username, "password" => $password, "email" => $email);
else
$json[] = array("name" => $name, "username" => $username, "password" => $password, "email" => $email);
//Encode the array back into a JSON string.
$json = json_encode($json);
//Save the file.
file_put_contents('user_doc.json', $json);
}
}else{
echo "error";
}
我能够成功发布请求但是我收到了php错误,请帮助我是php的新手... logcat如下所示:
D/success: <br><table border='1' cellpadding='2' bgcolor='#FFFFDF' bordercolor='#E8B900' align='center'><tr><td><font face='Arial' size='1' color='#000000'><b>PHP Error Message</b></font></td></tr></table><br />