0

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:\xampp\htdocs\server\userpage.php on line 27

Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\server\userpage.php on line 28

Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\server\userpage.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');
     }
       
}
?>
Mejo
  • 41
  • 5
  • Does [this](https://stackoverflow.com/a/65847778/128761) or [this](https://stackoverflow.com/a/61312351/128761) help? – vee Dec 04 '21 at 11:35
  • I try to check if the data fetched and it is return nothing but I have no idea. When I run the same query in database it is run and work, I think the wrong in fetching data. So do you have any idea how I can fix it ? – Mejo Dec 04 '21 at 12:06
  • https://www.php.net/manual/en/mysqli-result.fetch-array.php Document said return `null` if no result, return `false` on failure. Try to `var_dump($info);` to see what exactly is it. – vee Dec 04 '21 at 12:20
  • In case it is `false` then try to use [`mysqli_error()`](https://www.php.net/manual/en/mysqli.error.php) to see what errors is it. If it is `null` then try to dump `$query` and run it in somewhere else like phpMyAdmin. – vee Dec 04 '21 at 12:22
  • The problem is the id will be undefined when update.php file reload my page location in header , because I get the id from login page to user info page then when I update it and come back and retrieve the user data with id it will be undefined. How can I make the id defined in all pages? – Mejo Dec 04 '21 at 16:48
  • You get id from login page via method `POST` , when updated and come back and reload it is possible that it changed to method `GET`. Try to send this id using another way such as by query string `?loginID=nnn` instead. – vee Dec 04 '21 at 16:52

1 Answers1

1

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.

vee
  • 4,506
  • 5
  • 44
  • 81