Home > database >  PHP not updating the mysql database
PHP not updating the mysql database

Time:12-23

I have a form that user can update their profile details. But when the user clicks the update button only two fields are updating. I have 6 input fields and only 2 updating. There is not errors in my code. I can't find the problem.

My form is like this:
form

My database image:
db

My PHP code:

<?php include "includes/header.php" ?>
<?php include "includes/db.php" ?>

<br><br><br><br>
<?php //include "includes/navigation.php" 
?>



<div id="layoutSidenav_content">
    <main>
        <div >
            <h1 >Profile Settings </h1>

            <?php
            if (isset($_SESSION['username'])) {
                $username = $_SESSION['username'];

                $query = "SELECT * FROM users WHERE username = '{$username}' ";
                $select_user_profile_query = mysqli_query($connection, $query);

                while ($row = mysqli_fetch_array($select_user_profile_query)) {
                    $user_id = $row['user_id'];
                    $username = $row['username'];
                    $user_password = $row['user_password'];
                    $user_firstname = $row['user_firstname'];
                    $user_lastname = $row['user_lastname'];
                    $user_email = $row['user_email'];
                    $user_image = $row['user_image'];
                }
            }

            if (isset($_POST['update_profile'])) {
                $user_firstname = $_POST['user_firstname'];
                $user_lastname = $_POST['user_lastname'];

                $user_image = $_FILES['image']['name'];
                $user_image_temp = $_FILES['image']['tmp_name'];

                $username = $_POST['username'];
                $user_email = $_POST['user_email'];
                $user_password = $_POST['user_password'];

                move_uploaded_file($user_image_temp, "images_users/$user_image");

                if (empty($user_image)) {
                    $query = "SELECT * FROM users WHERE username = '{$username}' ";
                    $select_image = mysqli_query($connection, $query);
                    while ($row = mysqli_fetch_array($select_image)) {
                        $user_image = $row['user_image'];
                    }
                }

                $query = "UPDATE users SET ";
                $query .= "username = '{$username}', ";
                $query .= "user_password = '{$user_password}', ";
                $query .= "user_firstname = '{$user_firstname}', ";
                $query .= "user_lastname = '{$user_lastname}', ";
                $query .= "user_email = '{$user_email}', ";
                $query .= "user_image = '{$user_image}' ";
                $query .= "WHERE username = '{$username}' ";

                $update_post = mysqli_query($connection, $query);
                if(!$update_post) {
                    echo "Failed" . mysqli_error($connection);
                }
            }


            ?>

            <form action="" method="post" enctype="multipart/form-data">
                <div >
                    <div >
                        <div >
                            <label for="title">First Name</label>
                            <input value="<?php echo $user_firstname ?>" type="text" name="user_firstname" >
                        </div>
                    </div>
                    <div >
                        <div >
                            <label for="author">Last Name</label>
                            <input value="<?php echo $user_lastname ?>" type="text" name="user_lastname" >
                        </div>
                    </div>
                </div>

                <div >
                    <label for="post_status">Username</label>
                    <input value="<?php echo $username ?>" type="text" name="username" >
                </div>

                <br>

                <div >
                    <label for="post_image">User Image</label>
                    <br>
                    <img width="50" src="images_users/<?php echo $user_image; ?>" alt="image">
                    <br>
                    <br>
                    <input type="file" name="image">
                </div>
                <br>

                <div >
                    <label for="post_tags">Email</label>
                    <input value="<?php echo $user_email ?>" type="email" name="user_email" >
                </div>

                <br>

                <div >
                    <label for="post_tags">password</label>
                    <input autocomplete="off" type="password" name="user_password" >
                </div>

                <br>

                <div >
                    <input type="submit" value="Update Profile"  name="update_profile">
                </div>

            </form>



            <!-- <form action="" method="post" enctype="multipart/form-data">

                    <div >
                        <label for="title">First Name</label>
                        <input value="<?php echo $user_firstname ?>" type="text" name="user_firstname" >
                    </div>


                    <br>

                    <div >
                        <label for="author">Last Name</label>
                        <input value="<?php echo $user_lastname ?>" type="text" name="user_lastname" >
                    </div>

                    <br>

                    <div >
                        <label for="post_status">Username</label>
                        <input value="<?php echo $username ?>" type="text" name="username" >
                    </div>

                    <br>

                    <div >
                        <label for="post_image">User Image</label>
                        <br>
                        <img width="50" src="../images_users/<?php echo $user_image; ?>" alt="image">
                        <br>
                        <br>
                        <input type="file" name="image">
                    </div>
                    <br>

                    <div >
                        <label for="post_tags">Email</label>
                        <input value="<?php echo $user_email ?>" type="email" name="user_email" >
                    </div>

                    <br>

                    <div >
                        <label for="post_tags">password</label>
                        <input autocomplete="off" type="password" name="user_password" >
                    </div>

                    <br>

                    <div >
                        <input type="submit" value="Update Profile"  name="update_profile">
                    </div>

                </form> -->


        </div>


        <?php include "includes/footer.php" ?>

First name and the last name are updating. But other fields are not updating.

Thanks in advance!

CodePudding user response:

I see the problem on your code. You are updating information related to the same username that user input to the username. Instead of checking username in the query, check for user_id

$query .= "WHERE user_id = '{$user_id}' ";

Like above. I hope ur problem fixed. Thanks!

  • Related