Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于PHP和JSON的CRUD操作_Php_Json - Fatal编程技术网

基于PHP和JSON的CRUD操作

基于PHP和JSON的CRUD操作,php,json,Php,Json,我有一个表格,上面有姓名、图像、性别和名称字段。 我将这些表单变量存储在Json文件中 我将图像存储在uploads文件夹中,并在json文件中更新表单输入和图像路径 [{"name":"John", "image":"uploads/john-2019061656551615.jpg", "gender":male, "designation":developer}, {"name":"Russel", "image":"uploads/russel-201906161515.jpg", "g

我有一个表格,上面有姓名、图像、性别和名称字段。 我将这些表单变量存储在Json文件中

我将图像存储在uploads文件夹中,并在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"]  
                );  
                $array_data[] = $extra;  
                $final_data = json_encode($array_data);  
                if(file_put_contents('data.json', $final_data))  
                {  
                     $message = "<label class='text-success'>File Appended Success fully</p>";  
                }
           }  
           else  
           {  
                $error = 'JSON File not exits';  
           }        
    } else {
        echo "Sorry, there was an error uploading your file.";
    }   
    }
?>
<?php if (isset($_GET["id"])): ?>
    <form action="" method="POST">
        <input type="hidden" value="<?php echo $id ?>" name="id"/>
        <input type="text" value="<?php echo $jsonfile["name"] ?>" name="name"/>
        <input type="text" value="<?php echo $jsonfile["gender"] ?>" name="gender"/>
        <input class="button button2" type="file" name="fileToUpload" id="fileToUpload">
        <input type="text" value="<?php echo $jsonfile["designation"] ?>" name="designation"/>
        <input type="submit" value="Submit" name="submit">
    </form>
<?php endif; ?>
if(isset($\u GET[“id”])){
$id=(int)$\u 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[“性别”]=isset($_post[“性别”])?$_post[“性别”]:“”;
$post[“fileToUpload”]=isset($\u post[“fileToUpload”])?$\u post[“fileToUpload”]:“”;
$post[“指定”]=isset($_post[“指定”])?$_post[“指定”]:“”;
}
$target_dir=“uploads/”;
$target\u file=$target\u dir。basename($_文件[“fileToUpload”][“name”]);
如果(isset($_POST[“提交”])){
如果(移动上传的文件($文件[“fileToUpload”][“tmp文件名”],$target文件)){
回显“文件”。已上载basename($\u文件[“fileToUpload”][“name”]);
如果(文件_存在('data.json'))
{  
$current_data=file_get_contents('data.json');
$array\u data=json\u decode($current\u data,true);
$extra=数组(
'name'=>$\u POST[“name”],
'image'=>$target\u文件,
“性别”=>$\u POST[“性别”],
“指定”=>$\u POST[“指定”]
);  
$array_data[]=$extra;
$final_data=json_encode($array_data);
if(文件内容('data.json',$final\u data))
{  
$message=“文件附加成功”

”; } } 其他的 { $error='JSON文件不存在'; } }否则{ echo“抱歉,上载文件时出错。”; } } ?>
而不是推送新数据
$array\u data[]=$extra您可以维护唯一的记录id,并在数组中搜索该id并更新该特定数组

像下面这样

[{“uid”:100,name:“John”,“image:“uploads/John-20190616551615.jpg”, “性别”:男性,“名称”:开发者},{“uid”:101,“姓名”:“Russel”, “图片”:“uploads/russel-2019061515.jpg”,“性别”:男性, “名称”:developer},{“uid”:102,“名称”:“Jason”, “图片”:“上传/jason-20190616451657.jpg”,“性别”:男性, “指定”:开发商}]

函数searchForId($id,$array){
foreach($key=>$val的数组){
如果($val['uid']=$id){
返回$key;
}
}
返回null;
}
如果(isset($\u GET[“id”])){
$id=(int)$\u 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[“性别”]=isset($_post[“性别”])?$_post[“性别”]:“”;
$post[“fileToUpload”]=isset($\u post[“fileToUpload”])?$\u post[“fileToUpload”]:“”;
$post[“指定”]=isset($_post[“指定”])?$_post[“指定”]:“”;
}
$target_dir=“uploads/”;
$target_file=$target_dir.basename($_FILES[“fileToUpload”][“name”]);
如果(isset($_POST[“提交”])){
如果(移动上传的文件($文件[“fileToUpload”][“tmp文件名”],$target文件)){
回显“文件”。已上载basename($\u文件[“fileToUpload”][“name”]);
如果(文件_存在('data.json'))
{  
$current_data=file_get_contents('data.json');
$array\u data=json\u decode($current\u data,true);
$key=searchForId($id,$array\u数据);
$array_data[$key]['name']=$\u POST[“name”];
$array_data[$key]['image']=$target_文件;
$array_data[$key]['gender']=$\u POST[“gender”];
$array_data[$key]['designation']=$\u POST['designation'];
$final_data=json_encode($array_data);
if(文件内容('data.json',$final\u data))
{  
$message=“文件附加成功”

”; } } 其他的 { $error='JSON文件不存在'; } }否则{ echo“抱歉,上载文件时出错。”; } }
那个“json”文件“不是JSON,因此任何建立在该假设基础上的东西都会失败。也就是说,你需要提取并提供一个答案,如果没有这个答案,你的问题是离题的。作为这里的一个新用户,也请阅读下面的代码。上面的代码对我来说不起作用,但它展示了一种解决问题的方法。谢谢你的帮助。。。我还有一个疑问,我们可以提取图像路径并回显“file type”输入字段的值吗?就像我们对姓名、性别和名称所做的那样,您可以使用$name=$\u文件[“fileToUpload”][“name”]$类型=结束((分解(“.”,$name));
function searchForId($id, $array) {
       foreach ($array as $key => $val) {
           if ($val['uid'] === $id) {
               return $key;
           }
       }
       return null;
    }

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);

                $key = searchForId($id, $array_data);
                $array_data[$key]['name']= $_POST["name"];
                $array_data[$key]['image']= $target_file;
                $array_data[$key]['gender']= $_POST["gender"];
                $array_data[$key]['designation']= $_POST["designation"];  

                $final_data = json_encode($array_data);  
                if(file_put_contents('data.json', $final_data))  
                {  
                     $message = "<label class='text-success'>File Appended Success fully</p>";  
                }
           }  
           else  
           {  
                $error = 'JSON File not exits';  
           }        
      }else {
          echo "Sorry, there was an error uploading your file.";
      }   
    }


<?php if (isset($_GET["id"])): ?>
    <form action="" method="POST">
        <input type="hidden" value="<?php echo $id ?>" name="id"/>
        <input type="text" value="<?php echo $jsonfile["name"] ?>" name="name"/>
        <input type="text" value="<?php echo $jsonfile["gender"] ?>" name="gender"/>
        <input class="button button2" type="file" name="fileToUpload" id="fileToUpload">
        <input type="text" value="<?php echo $jsonfile["designation"] ?>" name="designation"/>
        <input type="submit" value="Submit" name="submit">
    </form>
<?php endif; ?>