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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:34:12+00:00 2026-05-17T02:34:12+00:00

I was wondering if anyone could take a look at this code and help

  • 0

I was wondering if anyone could take a look at this code and help me come up with a better way to do this if there is one.

I am using jQuery append/remove to add and remove list items. My code first selects any existing rows from the db and displays those in the list, and then the user can add or remove any elements from the list.

I have a database structure similar to this:

object_id  element_id   auto   order
   1          1          0       2
   1          2          0       1
   1          3          1       3

After users have finished appending/remove I will be left with html code similar to this:

<ul id="element-list" class="ui-sortable"><li>
<label for="element">Element Description</label>
<input type="hidden" value="1" name="element[1][id]">
<input type="checkbox" value="" name="element[1][auto]">
</li>

<li>
<label for="element">Element Description</label>
<input type="hidden" value="2" name="element[2][id]">
<input type="checkbox" value="" name="element[2][auto]">
</li>
</ul>

As users can add and remove elements including rows which exist in the database I am puzzled about how it is best to save these changes in the database. Currently I am deleting all corresponding rows then inserting new rows inside a loop as follows:

foreach($_POST['element'] as $element){

$auto = (isset($element["auto"])) ? 1 : 0;

$query="INSERT INTO $table (object_id, element_id, auto) VALUES ('".$object_id."', '".$element["id"]."', '".$auto."') ";
}

I am wondering if there is a more efficient way to do this than deleting all rows then inserting new ones?

Any input would be appreciated.

Thanks

Paul

  • 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-17T02:34:13+00:00Added an answer on May 17, 2026 at 2:34 am

    That’s definitely the easiest way to do it.

    2 suggestions…

    1. Use PDO or mysqli with prepared queries. Your code is vulnerable to sql injection.
    2. Use transactions. It would suck if you deleted all the rows and then somethign happened and the new rows werent inserted. This way you can rollback if the insertions arent completed.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Wondering if anyone can help me with this annoying but trivial (in terms of
I was just wondering if anyone could help out with how to do the
just wondering if anyone could suggest why I might be getting an error? I'm
I was wondering if anyone could point to an Open Source date utility class
I'm relatively new to ASP.NET and was wondering if anyone could shed some light
Just wondering if anyone knew off the top of their heads if there was
Im wondering if anyone has any input on this subject? Im building a flash
I was wondering if anyone knew of any limitations to using Windows XP as
High, I am wondering if anyone could give me some input on a design
I was wondering if anyone knows of a way I can Authenticate a Google

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.