Editing/updating data in server is unexceptional. In daily basis, we make use of Facebook post edit feature, changing password, name, picture, age and much more.

php mysql update multiple fields







I like to tell everyone following this blog or reading through my posts that this blog is not for entertainment but to educate and develop you for future purpose. So don’t expect funny write up when reading through my blog posts.


some useful tutorials:





So quickly let’s go into explaining the code below.
In this tutorial I assured you already have your user data inserted. What we are dealing with is to edit/update those user information.





Note: I’m going to do this in a way that each user limited to only updating his/her own data. So in this case each user has unique ID. The ID is what I will use in manipulating the entire edit system here.

Only signed in user can update his/her profile, which means non-sign in users can’t access this particular feature.

So first I wrote php script, start session and check if true the user is login else send the user back to index.php file. If the user is login, then store the user ID in php variable ($logged).

Next I connect my database connection after that, I stored the form data and forward them over to the server.
The main logistic here is I fetched data from the user table according to the signed in user ID. So in this case I got held of the user information and echo them out in the input fields according to their labels using HTML value attribute.

Now any user that login, his/her information will appear here.

Lastly I wrote MYSQL UPADTE statement the re-insert those value in the input fields back to the same users_table. First grape the ID of the login user and insert it according to that very user row in the table.

Note: All this code must be save in same file, example (edit.php).

<?php
//start session
session_start();

if(!isset($_SESSION['user_id']))
{
          header("Location: index.php");
}
else
{
          $logged_id = $_SESSION['user_id'];
}
$mydb = new mysqli("localhost", "root", "", "bulletin");
                                     
  if(isset($_POST['submit']))
    {      
     $user_first_name = $_POST['update_user_first_name'];
     $user_surn_name = $_POST['update_user_surn_name'];
     $user_email = $_POST['update_user_email'];
                                      $user_password = $_POST['update_user_password'];
                                     
     $user_first_name = $mydb->real_escape_string($user_first_name);
     $user_surn_name = $mydb->real_escape_string($user_surn_name);
     $user_email = $mydb->real_escape_string($user_email);
     $user_password = $mydb->real_escape_string($user_password);
                                     
     $su = $mydb->prepare("UPDATE users_table SET
     user_first_name = '{$user_first_name}',
     user_surn_name = '{$user_surn_name}',
     user_email = '{$user_email}',
     user_password = '{$user_password}' WHERE user_id='{$logged_id}'");
     $su->execute();
                                     
     if($su)
        {
             header("Location: profile.php");
        }
        else
        {       
            $errorreporter = "Error: retry again. Thank you!";
        }
        }
?>
<!DOCTYPE html>
<html lang="en">
<head>
          <title>settings</title>
          <style type="text/css">
                   .form-container-div-ele
                   {
                             width: 95%;
                             height: auto;
                             background-color: white;
                             text-align: center;
                             margin-left: auto;
                             margin-right: auto;
                             border-radius: 15px;
                   }
                   .signup-form-class input.input-field-ele, .file-field-ele
                   {
                             width: 90%;
                             height: 20px;
                             border-radius: 5px;
                   }
                   .signup-form-class input.submit-button
                   {
                             width: 50%;
                             height: 25px;
                             background-color: #02a9ce;
                             color: white;
                             border: none;
                             font-weight: bold;
                             border-radius: 5px;
                             cursor: pointer;
                   }
          </style>
</head>
<body>
<h3>edit account</h3>

<?php
    if(isset($_SESSION['user_id']))
   {
   $logged_id = $_SESSION['user_id'];
   $find = "SELECT * FROM users_table
   WHERE user_id='{$logged_id}'";
                                     
   $result = $mydb->query($find);
                                     
   if($row = $result->fetch_assoc())
  {
  }
  }
 else
 {
 header("Location: index.php");
 }
 ?>
                            
<form action="" method="POST" class="signup-form-class">                                       
<label>First Name:</label><br />
<input type="text" name="update_user_first_name" value="<?php echo $row["user_first_name"];?>" required="required" class="input-field-ele" /><br /><br />
                                               
<label>Surn Name:</label><br />
<input type="text" name="update_user_surn_name" value="<?php echo $row["user_surn_name"];?>" required="required" class="input-field-ele" /><br /><br />
                                               
<label>Email Address:</label><br />
<input type="email" name="update_user_email" value="<?php echo $row["user_email"];?>" required="required" class="input-field-ele" /><br /><br />
                                               
<label>Password:</label><br />
<input type="password" name="update_user_password" value="<?php echo $row["user_password"];?>" required="required" minlength="8" class="input-field-ele" /><br /><br />
                                               
<input type="submit" name="submit" value="update account" class="submit-button" /><br />
</form>
</body>
</html>
This code is tested and working 100%.

Questions are welcome here.