我有一个包含名称、图像、性别和名称字段的表单。我将这些表单变量存储在 Json 文件中。我将图像存储在上传文件夹中并更新表单输入,json 文件中的图像路径。[{"name":"John", "image":"uploads/john-2019061656551615.jpg", "gender":male, "designation":developer},{"name":"Russel", "image":"uploads/russel-201906161515.jpg", "gender":male, "designation":developer},{"name":"Jason", "image":"uploads/jason-20190616451657.jpg", "gender":male, "designation":developer}]我想在需要时更新 Json 文件。我尝试使用以下代码编辑 json 文件,但每次提交编辑表单时,它都会更新为新记录。if (isset($_GET["id"])) { $id = (int) $_GET["id"]; $getfile = file_get_contents('data.json'); $all = json_decode($getfile, true); $jsonfile = $all; $jsonfile = $jsonfile[$id]; $post["name"] = isset($_POST["name"]) ? $_POST["name"] : ""; $post["gender"] = isset($_POST["gender"]) ? $_POST["gender"] : ""; $post["fileToUpload"] = isset($_POST["fileToUpload"]) ? $_POST["fileToUpload"] : ""; $post["designation"] = isset($_POST["designation"]) ? $_POST["designation"] : "";}$target_dir = "uploads/";$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);if(isset($_POST["submit"])) { if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded."; if(file_exists('data.json')) { $current_data = file_get_contents('data.json'); $array_data = json_decode($current_data, true); $extra = array( 'name' => $_POST["name"], 'image' => $target_file, 'gender' => $_POST["gender"], 'designation' => $_POST["designation"] ); 如何仅编辑特定记录并在 json 文件中更新它?
1 回答
- 1 回答
- 0 关注
- 140 浏览
添加回答
举报
0/150
提交
取消