doutu7123 2017-04-25 03:49
浏览 94

如何通过php向json文件提交数据时避免重复?

I have a code that submits data into a cake json 'database', but when I submit using PHP. When I reload the page, the file repeats the code of the last object in the JSON file when I get it. How do I avoid this?

This is my PHP

if(isset($_POST["submit"]))  
 {  
      if(empty($_POST["name"]))  
      {  
           $error = "<label class='text-danger'>Enter Name</label>";  
      }  
      else if(empty($_POST["type"]))  
      {  
           $error = "<label class='text-danger'>Enter Type</label>";  
      }  
      else if(empty($_POST["diff"]))  
      {  
           $error = "<label class='text-danger'>Enter Difficulty</label>";  
      }  
      else  
      {  
           if(file_exists('../../databases/cakes.json'))  
           {  
                $current_data = file_get_contents('../../databases/cakes.json');  
                $array_data = json_decode($current_data, true);  
                $extra = array(
                     'person'       =>     array(
                        'name'         =>     $_POST['name'],  
                        'difficulty'   =>     $_POST["diff"],  
                        'type'         =>     $_POST["type"],
                        'isNew'        =>     'true',
                        'isVeg'        =>     'false',
                     )
                );  
                $array_data[] = $extra;  
                $final_data = json_encode($array_data);  
                if(file_put_contents('../../databases/cakes.json', $final_data))  
                {  
                     //.-. 
                }  
           }  
           else  
           {  
                $error = 'JSON File not exits';  
           }  
      }  
 }  
 ?>
<body>
<div id="layout"></div>
<div id="content">
<div id="add">
    <div class="form-title"><h1>Add Cake</h1></div>
    <form method="post">  
        <?php   
        if(isset($error))  
        {  
            echo $error;  
        }  
        ?>  
        <br />
        <div class="input-field">
            <label for="name">Cake Name</label>  
            <input type="text" name="name"/>
        </div>
        <br />
        <div class="input-field">
            <label for="diff">Difficulty</label>
            <div class="select">
            <select name="diff"  id="slct">
                <option>Choose an option</option>
                <option value="male">EZ</option>
                <option value="female">Meh</option>
                <option value="matthew">Mildy Hard</option>    
            </select>
            </div>
        </div>
        <br />
        <div class="input-field">
            <label for="type">Type</label>  
            <input type="text" name="type"/><br />
            <a href="#" class="pw">Need Suggestions?</a><br>
        </div>
        <input class="addCake" type="submit" name="submit" value="Add Cake!"/><br />
        <a href="#">See some other cakes</a>                    
        <?php  
        if(isset($message))  
        {  
            echo $message;  
        }  
        ?>  
    </form>

Submission Works. Result:

[[{"cake":{"name":"tes1","diff":"EZ","type":"Deli","isNew":"true","isVeg":"false"}}]]

But when I reload the page I see two of this things... Result:

[[{"cake":{"name":"tes1","diff":"EZ","type":"Deli","isNew":"true","isVeg":"false"},{"name":"tes1","diff":"EZ","type":"Deli","isNew":"true","isVeg":"false"}}]]
  • 写回答

2条回答 默认 最新

  • douyuan4697 2017-04-25 03:57
    关注

    Use ($_SERVER['REQUEST_METHOD'] == 'POST') instead of ($_POST["submit"])

    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错