doudilin1225 2017-12-09 21:00
浏览 86

JSon数组从$ _POST ['id']增加我的id值

In my php script, I have managed to get all the data from a form into a json database, however everytime I submit my form, the id value is always == 1. How would I be able to change this, and add more values to my id value, like value++? How would I be able to do that?

<?php  
$message = '';  
 $error = '';  
 if(isset($_POST["submit"]))  
 {  
  if(empty($_POST["title"]))  
  {  
       $error = "<label class='text-danger'>Enter title</label>";  
       echo $error;
  }  
  else if(empty($_POST["decs"]))  
  {  
       $error = "<label class='text-danger'>Enter Gender</label>";  
       echo $error;
  }  
  else if(empty($_POST["cont"]))  
  {  
       $error = "<label class='text-danger'>Enter Designation</label>";  
       echo $error;
  }  
  else  
  {  
       if(file_exists('postbl.json'))  
       {  

            $current_data = file_get_contents('postbl.json');  
            $array_data = json_decode($current_data, true);  
            $extra = array(  
                 'id'               =>     $_POST['id'], 
                 'title'            =>     $_POST['title'],  
                 'decs'             =>     $_POST["decs"],  
                 'cont'             =>     $_POST["cont"]  
            );  
            $array_data[] = $extra;  
            $final_data = json_encode($array_data); 

            if(file_put_contents('postbl.json', $final_data))  
            {  
                 $message = "<label class='text-success'>File Appended Success fully</p>"; 
                 echo $message; 
            }  
       }  
       else  
       {  
            $error = 'JSON File not exits';  
       }  
  }  
 }  
 ?>  

my form is like this

<?php session_start();
if(!isset($_SESSION['loggedin'])) header("Location: session.php");
if($_SESSION['loggedin']===FALSE) header("Location: session.php");
?>
<!DOCTYPE html>
<html><head><title>Secret Area</title></head>
<body>
<form action="/admin/form_process.php" method="POST">
<br />
<input type="hidden" name="id" value="1" />
<label>Title</label>
 <input type="text" name="title" class="form-control" /><br /> 
<label>Description</label>
 <input type="text" name="decs" class="form-control" /><br /> 
<label>Content</label>
 <input type="text" name="cont" class="form-control" /><br /> 
<input type="submit" name="submit" value="Append" class="btn btn-info" /><br />
  <?php  
                 if(isset($message))  
                 {  
                      echo $message;  
                 }  
                 ?>
</form>
<form action="logout.php" method="POST">
 <input type="submit" name="logout" value="Log out">
</form>
<p>&copy; 2017 Blog Message</p>
</body>
 </html>
  • 写回答

1条回答 默认 最新

  • dopr25398 2017-12-09 22:31
    关注

    Your problem is here

    <input type="hidden" name="id" value="1" />
    

    Notice how the 1 is set as value.

    Change the 1 with a variable. Something like

    <input type="hidden" name="id" value="<?= $id ?>" />
    
    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)