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

时间:2017-12-09 21:00:08

标签: javascript php jquery arrays json

在我的php脚本中,我设法将表单中的所有数据都收集到json数据库中,但每次提交表单时,id值始终为== 1。我如何更改此设置,并为我的id值添加更多值,例如value++?我怎么能这样做?

<?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';  
       }  
  }  
 }  
 ?>  

我的表格就像这样

<?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 个答案:

答案 0 :(得分:1)

你的问题在这里

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

注意1如何设置为值。

使用变量更改1。像

这样的东西
<input type="hidden" name="id" value="<?= $id ?>" />