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 7795783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:10:05+00:00 2026-06-01T23:10:05+00:00

I wanted to minimize sql queries as possible in order to save some load

  • 0

I wanted to minimize sql queries as possible in order to save some load on my server. I have this mysql queries when a form is submitted,

$updateglobal_products_data = array(
                        'products_type' => tep_db_prepare_input($HTTP_POST_VARS['products_type']),
                        'global_category_id' => tep_db_prepare_input($HTTP_POST_VARS['global_categories']),
                        'display_product' => tep_db_prepare_input($HTTP_POST_VARS['display_product']),
                        'products_description' => tep_db_prepare_input(strip_tags($HTTP_POST_VARS['products_description'][1])),
                        'products_name' => tep_db_prepare_input(stripslashes($HTTP_POST_VARS['products_name'][1])),
                        'products_image' => $products_image->filename,
                        'products_last_modified' => tep_db_prepare_input($gcurrent_datetime),
                        'products_quantity' => (int)tep_db_prepare_input($HTTP_POST_VARS['products_quantity']),
                        'products_model' => tep_db_prepare_input($HTTP_POST_VARS['products_model']),
                        'products_price' => $products_price,
                        'products_date_available' => $products_date_available,
                        'products_weight' => (float)tep_db_prepare_input($HTTP_POST_VARS['products_weight']),
                        'products_status' => tep_db_prepare_input($HTTP_POST_VARS['products_status']),
                        'products_tax_class_id' => tep_db_prepare_input($HTTP_POST_VARS['products_tax_class_id']),
                        'manufacturers_id' => (int)tep_db_prepare_input($HTTP_POST_VARS['manufacturers_id']));

$sql_data_array = array('products_quantity' => (int)tep_db_prepare_input($HTTP_POST_VARS['products_quantity']),
                        'products_type' => tep_db_prepare_input($HTTP_POST_VARS['products_type']),
                        'img_display' => tep_db_prepare_input($HTTP_POST_VARS['image_display']),
                        'products_model' => tep_db_prepare_input($HTTP_POST_VARS['products_model']),
                        'products_price' => tep_db_prepare_input($HTTP_POST_VARS['products_price']),
                        'products_date_available' => $products_date_available,
                        'products_weight' => (float)tep_db_prepare_input($HTTP_POST_VARS['products_weight']),
                        'products_status' => tep_db_prepare_input($HTTP_POST_VARS['products_status']),
                        'products_tax_class_id' => tep_db_prepare_input($HTTP_POST_VARS['products_tax_class_id']),
                        'manufacturers_id' => (int)tep_db_prepare_input($HTTP_POST_VARS['manufacturers_id']));

$update_sql_data = array('products_last_modified' => 'now()');
$sql_data_array = array_merge($sql_data_array, $update_sql_data);

  $wpdb->update( TABLE_PRODUCTS, $sql_data_array, array( 'products_id' => $products_id ));
  $wpdb->update( TABLE_GLOBAL_PRODUCTS, $updateglobal_products_data, array( 'blog_id' => $blog_id, 'products_id' => $products_id ));

  $delete_rps = "DELETE from " . TABLE_RELATED_PRODUCTS . " where products_id = '" . $products_id . "'";
  $wpdb->query("DELETE from " . TABLE_RELATED_PRODUCTS . " where products_id = '" . $products_id . "'");

  if(mysql_query($delete_rps)) {
    foreach($insert_rp_ids1 as $id){ 
    $rps_each2 = array('products_id' => $products_id, 'related_products_ids' => $id);
    $wpdb->insert(TABLE_RELATED_PRODUCTS, $rps_each2);
    }
  }

$wpdb->query("DELETE from " . TABLE_RELATED_PRODUCTS . " where related_products_ids = '" . $products_id . "'");

foreach($insert_rp_ids1 as $rp_ids)
 {

$result_rp = mysql_query("SELECT related_products_ids, products_id FROM ".TABLE_RELATED_PRODUCTS." where products_id = '" . $rp_ids ."'");
 if(!mysql_num_rows($result_rp))
   {
     $rps_each2 = array('products_id' => $rp_ids, 'related_products_ids' => $products_id);
     $wpdb->insert(TABLE_RELATED_PRODUCTS, $rps_each2);

   }
 }

Is there a way to minimize these sql queries? or is my code okay?

  • 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-06-01T23:10:07+00:00Added an answer on June 1, 2026 at 11:10 pm

    Generally speaking, this code is a little hard to understand. Where are the comments? It seems you are doing several operations involving more than one table, but it’s not obvious exactly what.

    Some thoughts:

    You are looping twice over $insert_rp_ids1 – can you merge these loops? Would be better to have distinct INSERT, UPDATE and DELETE sections, clearly labelled with comments IMO.

    You are inserting only if delete is successful. Why not insert either way?

    Additionally, some of your variable names are a bit opaque. What exactly are $rps_each2 and $insert_rp_ids1 anyway? Names like $listOfProductIDsToInsert are more descriptive and therefore more maintainable for the future, especially if someone else has to deal with this code.

    Your indenting is a bit funny. Assuming this is not a copy/paste error, this matters in making the code readable, so it needs fixing.

    Other than that, your SQL seems quite compact and efficient.

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

Sidebar

Related Questions

I have an ASP.NET MVC Web Application that interacts with a SQL Server 2008
Wanted to get some consensus around a UI feature I'm working on right now.
Wanted some help with a problem with mapkit I am facing. Should be a
I have been stumped on this one for a while. I want to take
Wanted to know, because side effect of this could be to alert user that
I just wanted to seek some advice on database design with mongoid and rails.
I wanted to check whether g++ supports tail calling so I wrote this simple
I wanted to ask about the best way to write this JQUERY code. I
so I wanted to have a table with multiple sections and each one with
Wanted some ideas about building a tool which can scan text sentences (written in

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.