Skip to content
Advertisement

display the same form with new updated data after click on save button in PHP

I have a form that show the user information and when I load userpage.php all info appear in each field and user can edit his information all is good until the user click on save button, it have to show the same form with updated information, data updated in database but the new data doesn’t appear when user click save.

This is the form fields values after click on save button:


Notice: Trying to access array offset on value of type null in C:xampphtdocsserveruserpage.php on line 27

Notice: Trying to access array offset on value of type null in C:xampphtdocsserveruserpage.php on line 28

Notice: Trying to access array offset on value of type null in C:xampphtdocsserveruserpage.php on line 29

This is the userpage.php file:

       <?php
        session_start();
        include 'connection.php';
        ?>
        <!DOCTYPE html>
        <html>
        <head>
        <meta charset="utf-8">
        <title>My Account</title>
        </head>
    
        <body>
            <?php  
                   if(isset($_SESSION['Status'])){
                      echo "<h2>".$_SESSION['Status']."<h2>";
                      unset($_SESSION['Status']);
                    } 
               $id = $_POST['loginID'];//a get this from login page
               $_SESSION['nid'] = $id;  //To use it in update file
               $query = "SELECT id, username, phone, email FROM  user  WHERE id ='$id';";
               $result = mysqli_query($conn,$query);
               $info =mysqli_fetch_array($result);
        ?>
    
  <form id="profile" action="update.php" method="post">
    <fieldset>
      <input type="text" name="username" id="username" value="<?php echo $info['username'] ?>" >
      <input type="tel" name="phone" id="phone"  value="<?php echo $info['phone'] ?>" >
      <input type="email" name="email" id="email"  value="<?php echo $info['email'] ?>">
      <input class="button" type="submit" name="save" value="save">
    
   </fieldset>
               
 </form>
</body>
</html>

Update.php file code:

<?php
  session_start();
  include 'connection.php';
  
  if(isset($_POST['save'])){

         $id = $_SESSION['nid']; //Get the id from userpage.php file

         $phone = $_POST['phone'];
         $email = $_POST['email'];
       
         $query = "UPDATE user SET phone='$phone', email='$email'  WHERE id='$id'";
         $result = mysqli_query($conn,$query);
         if($result){
        $_SESSION['Status'] = "Updated";
         header('location: userpage.php');
     }
     else{
         $_SESSION['Status'] = "Not updated";
         header('location: userpage.php');
     }
       
}
?>

Advertisement

Answer

On your userpage.php. You get $id via method POST but in the page update.php you redirect back to userpage.php. The redirection is method GET that is why you lost the ID.

To prevent this, use session that is already set.
First, remove your 2 lines of code.

$id = $_POST['loginID'];//a get this from login page
$_SESSION['nid'] = $id;  //To use it in update file

And replace with this.

$id = ($_POST['loginID'] ?? null);// use null coalesce operator to prevent undefined index.
// the $_POST['loginID'] code above still get the value that send via method POST from login page.
if ($id !== '' && !is_null($id)) {
    // if id is not empty and not null. I don't check with empty() function to allow zero (0) value.
    // set it to session.
    $_SESSION['nid'] = $id;
} elseif (isset($_SESSION['nid'])) {
    // if session nid was set, use it.
    // this condition will work on redirected back.
    $id = $_SESSION['nid'];
} else {
    // if come to this condition, it means that you have no ID at all!
    // do whatever you want such as redirect to logout page for login again.
}

And then you can use $id as before.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement