当我进行 ajax 调用时,我试图在我的 php 中写入一个 json 文件。我要做的是将一个 JSON 编码数组写入 json 文件,而另一个将数据返回给用户。当我不尝试写入文件时,一切正常,但是当我打开 JSON 文件并尝试写入时,没有任何效果。谢谢你的帮助,猎人。
代码
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
$name = mysql_real_escape_string($_POST['name']);
$email = mysql_real_escape_string($_POST['email']);
$lat = mysql_real_escape_string($_POST['lat']);
$lon = mysql_real_escape_string($_POST['lon']);
$choose_it = array('it', 'not it');
$key = array_rand($choose_it);
$it = $choose_it[$key];
$all_names = array();
$all_emails = array();
$sql="SELECT uname AND email FROM accounts";
$r = mysql_query($sql);
while($row = mysql_fetch_array($r))
{
array_push($all_names, $row["name"]);
array_push($all_emails, $row["email"]);
}
if(isset($username) && isset($password) && isset($name) && isset($lat) && isset($lon) && isset($email))
{
if(in_array($name, $all_names))
{
$data = array("message" => "taken", "name" => "taken");
echo json_encode($data);
}
if(in_array($email, $all_emails))
{
$data = array("message" => "email", "name" => "email");
echo json_encode($data);
}
else
{
$query = "INSERT INTO accounts (name, uname, pword, email, lat, lon, it) VALUES ('".$name."','".$username."','".$password."','".$email."','".$lat."','".$lon."','".$it."')";
mysql_query($query);
$write_data = json_encode(array("name" => $name, "lat" => $lat, "lon" => $lon, "it" => $it));
$file = file_get_contents('');
$fh = fopen($file, 'a') or die("can't open file");
fwrite($fh, $write_data);
fclose($fh);
$data = json_encode(array("message" => "yes", "name" => $name, "it" => $it));
echo $write_data;
}
}
else
{
echo json_encode(array("message" => "not_set", "name" => "not_set"));
}
?>