【问题标题】:JSon array increment my id value from $_POST['id']JSON 数组从 $_POST['id'] 增加我的 id 值
【发布时间】:2017-12-09 21:00:07
【问题描述】:

在我的 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>

【问题讨论】:

  • 我有点困惑 $_POST['id'] 引用的位置,因为它不在您的代码中。
  • 你去吧,post id 是在我的表单中引用我的 id,我想要的是当它把数据写入 json 时,我希望 id 的值增加一个
  • 您需要编写代码从 JSON 文件中获取最后一个 ID,然后将其递增 1

标签: javascript php jquery arrays json


【解决方案1】:

你的问题来了

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

注意 1 是如何设置为值的。

用变量改变 1。类似的东西

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

【讨论】:

  • 以及如何让 $id 每次都增加
  • 如果你增加了会发生什么或者必须增加什么? 'id' => $_POST['id'] + 1,
  • 我想做的是创建一个博客网站,使用这个表单我可以创建一个数据库,我想为每个正在存储的表单提供 id,稍后我会得到它们与身份证号码。我尝试了 'id' => $_POST['id'] + 1 但是当数据输出到 postbl.json 时,每个数据的 id 仍然是 1 似乎无法改变它。
  • 你可以试试 ($_POST['id'] + 1) 和 () 否则,做一个变量叫 $newID = ($_POST['id'] + 1);然后做 'id' => $newID
  • 还是一样,它为每个 id 提供 1。有没有办法获取数组中的最后一个 id 只需执行 ++ 类似这样的操作 $last_item = end($data); $last_item_id = $last_item['id'];
猜你喜欢
  • 1970-01-01
  • 2021-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 2014-08-03
  • 2015-03-10
  • 1970-01-01
相关资源
最近更新 更多