简体   繁体   English

如何通过PHP中的用户输入将数据保存在JSON文件中

[英]How to save data in json file by user input in php

I am trying to get input from user and on submission it get save in Json file but i am experiencing errors and there solutions on web did not help me. 我正在尝试从用户那里获取输入,并在提交时将其保存在Json文件中,但是我遇到了错误,网络上的解决方案没有帮助我。

Here is my code: 这是我的代码:

<?php

array()
$arr_data[] = array(); // create empty array

$myFile = "sample.json";


try
 {
   //Get form data
   $formdata = array(
      'name'=> $_POST['name'],
      'age'=> $_POST['age'],
      'sex'=> $_POST['sex'],
      'education'=>$_POST['edu']

   );


  //Get data from existing json file
   $jsondata = file_get_contents($myFile);

   // converts json data into array
   $arr_data = json_decode($jsondata, true);

   // Push user data to array
   array_push($arr_data,$formdata);

   //Convert updated array to JSON
   $jsondata = json_encode($arr_data, JSON_PRETTY_PRINT);

   //write json data into data.json file
   if(file_put_contents($myFile, $jsondata)) {
        echo 'Data successfully saved';
    }
   else 
        echo "error";

   }

    catch (Exception $e) {
        echo 'Caught exception: ',  $e->getMessage(), "\n";
  }


   ?>

Now the form I am using code is here: 现在我正在使用代码的形式在这里:

 <form  id="myform" method="post" action="action.php" > 
                                <div class="row">
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label> Name</label>
                                            <input type="text" class="form-control" placeholder="Please type  Name" value="" name="name"  size="20" required>
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label>Age</label>

                                                <select class="form-control" placeholder="Please Select Education" value="" id="edu" name="edu" required>
                                                <option selected="true" disabled="disabled" >Please Select Type</option> 
                                                    <option value="eng">Engineer</option>
                                                    <option value="doc">Doctor</option>
                                                    <option value="res">Researcher</option>


                                                </select>

                                        </div>
                                    </div>

                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label>Age</label>
                                            <input type="number" class="form-control" placeholder="Please specify Age" value="age" id="age" name="age" size="20" required>
                                        </div>
                                    </div>


                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label>sex</label>
                                            <input type="text" class="form-control" placeholder="Please specify Sex" value="" id="sex" name="sex" size="50" required>
                                        </div>
                                    </div>

                                </div>

                               <button type="submit" class="btn btn-info btn-fill pull-right" id="btn" name="btn"> Submit</button>
                                <div class="clearfix"></div>
                            </form>

Now the issue is on submission. 现在问题已经提交。 I got following errors and nothing on web is helping me. 我遇到以下错误,网络上没有任何帮助。 I don't know where I am wrong. 我不知道我哪里错了。

Warning: array_push() expects parameter 1 to be array, null given in /Users/path/action.php on line 34 警告:array_push()期望参数1为数组,在第34行的/Users/path/action.php中给定null

Warning: file_put_contents(sample.json): failed to open stream: Permission denied in /Users/path/action.php on line 40 error 警告:file_put_contents(sample.json):无法打开流:/Users/path/action.php中第40行的权限被拒绝错误

Thanks in advance for your help and time. 在此先感谢您的帮助和时间。

All you need - just set CRUD permissions for file sample.json . 您所需要的-只需为文件sample.json设置CRUD权限即可。

In terminal run: 在终端运行中:

chmod -R 0777 /path-to/sample.json

This will fix both errors: 这将修复两个错误:

 $jsondata = file_get_contents($myFile); -->> (this returns NULL because of permissions issue)

file_put_contents($myFile, $jsondata) -->> (and this just causing fatal error - again due to permissions)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM