Melih Anik
Melih Anik

Reputation: 25

JSon array increment my id value from $_POST['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>

Upvotes: 0

Views: 195

Answers (1)

Mo D Genesis
Mo D Genesis

Reputation: 6159

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 ?>" />

Upvotes: 1

Related Questions