Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6133413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:13:32+00:00 2026-05-23T17:13:32+00:00

I know how to update the rows in my database, I know how to

  • 0

I know how to update the rows in my database, I know how to transfer data from one page to another, but what I don’t know is a good way to update the database of several different rows as long as the variable aren’t empty.

What I am working on is a user page with settings to change real name, username, password and similar, but I have no clue of how to just update the once I have changed. If I leave for example “Username” empty and press “Change Settings”, then it empties the row of username. How do I make it ignore the empty variables?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T17:13:32+00:00Added an answer on May 23, 2026 at 5:13 pm

    You most likely want to use prepared statements to accomplish this. Both to make the code prettier and more robust, and to avoid SQL injections (alternative).

    Now, since you want to update different columns based on some condition, you have a few alternatives on how to continue.

    One would be to use several update statements (one for each column you wish to update) and bundle them inside a transaction. Bundling the statements inside a transaction will ensure atomicity and will also help mitigate the performance impact of using several SQL statements in place of one (something that won’t likely matter unless you have many concurrent users).

    It would probably look something like this (using the mysqli API):

    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    $mysqli = new mysqli("dbhost", "dbuser", "dbpassword", "dbname");
    $mysqli->autocommit(FALSE);
    
    if ($userName) {
        $stmt = $mysqli->prepare("UPDATE TABLE users SET username=? WHERE uid=?");
        $stmt->bind_param("si", $userName, $uid);
        $stmt->execute();
    }
    
    if ($password) {
        $stmt = $mysqli->prepare("UPDATE TABLE users SET password=? WHERE uid=?");
        $stmt->bind_param("si", $password, $uid);
        $stmt->execute();
    }
    
    if ($firstName) {
        $stmt = $mysqli->prepare("UPDATE TABLE users SET firstname=? WHERE uid=?");
        $stmt->bind_param("si", $firstName, $uid);
        $stmt->execute();
    }
    
    if ($lastName) {
        $stmt = $mysqli->prepare("UPDATE TABLE users SET lastname=? WHERE uid=?");
        $stmt->bind_param("si", $lastName, $uid);
        $stmt->execute();
    }
    
    $mysqli->commit();
    $mysqli->close();
    

    For brevity I’ve left out the error handling; you’d need to check for connection failures and problems preparing the statements as well as the execution of them. If anything fails after the first execute() line you need to call $mysqli->rollback() to make sure no changes are performed.

    You would also be well advised to refactor the code so it doesn’t repeat itself quite so much, something like this perhaps:

    function updateColumn($mysqli, $query, $uid, $value) {
        if ($value) {
            $stmt = $mysqli->prepare($query);
            $stmt->bind_param("si", $value, $uid);
            $stmt->execute();
        }
    }
    
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    $mysqli = new mysqli("dbhost", "dbuser", "dbpassword", "dbname");
    $mysqli->autocommit(FALSE);
    
    updateColumn($mysqli, "UPDATE TABLE users SET username=? WHERE uid=?", $uid, $userName);
    updateColumn($mysqli, "UPDATE TABLE users SET password=? WHERE uid=?", $uid, $password);
    updateColumn($mysqli, "UPDATE TABLE users SET firstname=? WHERE uid=?", $uid, $firstName);
    updateColumn($mysqli, "UPDATE TABLE users SET lastname=? WHERE uid=?", $uid, $lastName);
    
    $mysqli->commit();
    $mysqli->close();
    

    You could take it a few steps further and generate the query as well, that’s rather safe since you are not depending on/using user input to generate the query. This would get hairy fairly quickly though.

    The other ways would be the ones already mentioned here, or combining the dynamic generation of the query with prepared statements. You might also want to try to do the same with the PDO API.

    Probably the best way in the long term would probably be to use a third-party ORM framework (see this related question for suggestions).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to migrate data from one database to another. As preparation we have
update: I know there is no one best way to do everything. Sorry for
I'm trying to update multiple rows in one table in MySQL database by doing
I am looking for a way to know which rows in a database (mysql)
I want to know number of rows that will be affected by UPDATE query
I know I can update a single record like this - but then how
UPDATE: I know I can use <ol> directky in the output but I remember
Does anyone know what the esiest way to update the entity model after adding/deleting
I have this new challenge to load ~100M rows from an Oracle database and
I feel dumb right now. I got to update 100.000 rows on a database

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.