0

Complete beginner here, so I apologise in advance.

I've tried to look for similar questions and on the internet in general, but can't seem to find the answer.

I'm creating a profile page for my users where they can see and update their info.

I created a form that lets them do that, however I would like that if they leave an entry blank, it won't be updated in the db. (i.e. they can update just their password, or just their address, etc) and I can't seem to understand how to do this.

Here's the code:

<?php 
$mysqli = new mysqli('localhost', 'root', '', 'e-talian');
if(isset($_POST['submit'])){
    $stmt = $mysqli->prepare("SELECT * from user WHERE user_id='".$id."'");
    $stmt->execute();
    $result = $stmt->get_result();
    $sql = "UPDATE user SET user_name='".$_POST['user_name']."', user_last='".$_POST['user_last']."', user_email='".$_POST['email']."', 
user_location='".$_POST['user_location']."', user_phonenum='".$_POST['user_phonenum']."', 
user_password='".$_POST['user_password']."' WHERE user_id='$id'";
    $result = mysqli_query($conn, $sql);
       
}
?>

And the HTML:

<form action="" method='POST'>
      <div class="form-group">
        <label for="">First Name</label>
        <input type="text" name="user_name" id="timeslot" class="form-control">
        </div>
      <div class="form-group">
        <label for="">Last Name</label>
        <input type="text" name="user_last" class="form-control">
        </div>
        <div class="form-group">
        <label for="">Email</label>
        <input  type="email" name="email" class="form-control">
        </div>
        <div class="form-group">
        <label for="">Password</label>
        <input  type="password" name="user_password" class="form-control">
        </div>      
      <div class="form-group">
        <label for="">Phone Number</label>
        <input type="text" name="user_phonenum" class="form-control">
        </div>
      <div class="form-group">
        <label for="">Address</label>
        <input type="text" name="user_location" class="form-control">
        </div>      
        <div class="form-group pull-right">
        <button class="btn btn-primary" type="submit" name="submit" style="background: linear-gradient(-145deg, rgba(255, 216, 133,1) 0%, rgba(245, 164, 37,1) 100%);">Submit</button>
        </div>      
        </form>

You can see there's no condition at the moment.

I think I need an if statement of some sort that allows to update only the field that is not empty (or if the field is different from what already stored in the db), but I don't know how to phrase it.

Can anyone help? Thank you!

0 Answers0