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

  • Home
  • SEARCH
  • 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 6198229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:57:39+00:00 2026-05-24T03:57:39+00:00

I wonder whether this idea is a good practice to update a row in

  • 0

I wonder whether this idea is a good practice to update a row in a table in the database.

I usually update the row like this,

$pg_id = set_variable($_POST,'pg_id');
$pg_url = set_variable($_POST,'pg_url');
$pg_title = set_variable($_POST,'pg_title');
$pg_subtitle = set_variable($_POST,'pg_subtitle');
$pg_description = set_variable($_POST,'pg_description');
$pg_introduction = set_variable($_POST,'pg_introduction');
$pg_content_1 = set_variable($_POST,'pg_content_1');
$pg_content_2 = set_variable($_POST,'pg_content_2');
$pg_content_3 = set_variable($_POST,'pg_content_3');
$pg_content_4 = set_variable($_POST,'pg_content_4');
$pg_backdate = set_variable($_POST,'pg_backdate');
$pg_tag = set_variable($_POST,'pg_tag');
$pg_user = set_variable($_POST,'pg_user');
$pg_member = set_variable($_POST,'pg_member');
$pg_highlight = set_variable($_POST,'pg_highlight');
$pg_hide = set_variable($_POST,'pg_hide');
$pg_cat_id = set_variable($_POST,'pg_cat_id');
$ps_cat_id = set_variable($_POST,'ps_cat_id'); 
$parent_id = set_variable($_POST,'parent_id');
$tmp_id = set_variable($_POST,'tmp_id');
$usr_id = set_variable($_POST,'usr_id');

$sql = "
UPDATE root_pages
SET 
    pg_url = ?, 
    pg_title = ?,
    pg_subtitle = ?,
    pg_backdate = ?,
    pg_description = ?,
    pg_introduction = ?,        
    pg_content_1 = ?,
    pg_content_2 = ?,
    pg_content_3 = ?,
    pg_content_4 = ?,
    pg_highlight = ?,
    pg_hide = ?,
    ps_cat_id = ?,  
    parent_id = ?, 
    tmp_id = ?,
    updated_by = ?
WHERE pg_id = ?
";

# use the instantiated db connection object from the init.php, to process the query
$result = $connection->run_query($sql,array(
    $pg_url, 
    $pg_title,
    $pg_subtitle,
    $pg_backdate,
    $pg_description,
    $pg_introduction,
    $pg_content_1,
    $pg_content_2,
    $pg_content_3,
    $pg_content_4,
    $pg_highlight,
    $pg_hide,
    $ps_cat_id, 
    $parent_id, 
    $tmp_id,
    $usr_id,
    $pg_id
    ));

I find this is taking a long time to type to list all the fields in the table when comes to maintenance, so I have this short-cut idea to get around it,

# queury the table columns.
$sql = "
SHOW COLUMNS 
FROM root_pages
";

# use the stored connection object from the class_page_controller.php, to process the query.
$columns = $connection->fetch_all($sql);

# loop through the table columns, select the 'Field' column only, turn the field into variables, then get the variable's value from the array.
foreach($columns as $column)
{
    $$column['Field'] = set_variable($_POST,$column['Field']);
}

foreach($columns as $column)
{

    $sql = "
    UPDATE root_pages
    SET 
        ".$column['Field']." = ?
    WHERE pg_id = ?
    ";

    # use the instantiated db connection object from the init.php, to process the query
    $result = $connection->run_query($sql,array($$column['Field'],$pg_id));

}

It is shorter but I use loads of looping in this short-cut – is it bad?

Does this make the server slow in processing the update? What problems would I get in this method that I fail to see?

  • 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-24T03:57:40+00:00Added an answer on May 24, 2026 at 3:57 am

    More queries means more time, so if you are updating each field individually (rather than a row at a time), it will take significantly longer.

    Also, you may want to apply a filter to the submitted values to ensure that no field which you do not want updated can be.

    For instance, if you had a table of users who had account balances listed against them:

    id | user       | credit
    ==========================
    1  | John Smith | 50
    

    If I could submit a form to your form handler, as the “credit” field would show up in the SHOW COLUMNS... query, I could send you a POST submission, through a form intended to be used for me to change my name, with $_POST['user'] = "Mike Rowe" and $_POST['credit'] = 9999, and you would change the above to:

    id | user       | credit
    ==========================
    1  | Mike Rowe  | 9999
    

    UPDATE: Suggested solution

    Rather than trust that the database field names are safe to use for handling a query like this, why not have your own array of editable fields and just loop through them?

    $editable_fields = array(
      'pg_url' ,
      'pg_title' ,
      ...
    );
    
    $form_values = array();
    $sql_pattern = array();
    foreach( $editable_fields as $k ){
      if( $k != 'pg_id'
          && isset( $_POST[$k] ) ){
        $form_values[$k] = $_POST[$k];
        // NOTE: You could use a variant on your above code here, like so
        // $form_values[$k] = set_variable( $_POST , $k );
        $sql_pattern[] = "$k = ?";
      }
    }
    
    $sql_pattern = 'UPDATE root_pages SET '.implode( ' , ' , $sql_pattern ).' WHERE pg_id = ?';
    
    # use the instantiated db connection object from the init.php, to process the query
    $result = $connection->run_query($sql_pattern,array_merge(
        $form_values ,
        $_POST['pg_id']
        ));
    

    NOTE: This code is untested and not the way I usually operate, so use it as a guide, not a bible…

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

Sidebar

Related Questions

I wonder whether there is any automatic way of determining (at least roughly) the
I just wonder a bit whether or not GDI+ is still a technology worth
I wonder, whether it is possible to create class-methods in VBA. By class-method I
I wonder if someone knows if there is a pre-made solution for this: I
I wonder how you guys manage deployment of a database between 2 SQL Servers,
I wonder how long it would usually take for: Professional Average Beginner to setup
I wonder if there is any practice when designing a forum. I want to
I wonder whether it is possible to cast a non-Comparable to something so that
Wonder what the difference between: static PROCESSWALK pProcess32First=(PROCESSWALK)GetProcAddress(hKernel,Process32First); ... pProcess32First(...); what is hKernel? Look
I wonder if anyone uses commercial/free java obfuscators on his own commercial product. I

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.