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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:07:26+00:00 2026-06-08T04:07:26+00:00

I had my first question answered here: link My extended question is, I can

  • 0

I had my first question answered here:link

My extended question is, I can successfully update rows into the database but for some reason I cannot insert if the itemid = 0? code below, If I do it adds a further 8 rows?:

for ($i = 0; $i < count($values); $i++)
{
$item           = $values['item'][$i];
$description    = $values['description'][$i];
$rate           = $values['rate'][$i];
$itemid         = $values['itemid'][$i];

if($itemid)
{
    // Update item
    $query = "UPDATE `invoice_items`...";
}
else
{
    // Add new item
    $query = "INSERT INTO `invoice_items`...';
}

}

#

Update

#

Here is what I have now, But I am still inserting an additional 7 items to the database when I am only updating the item:

foreach ($outArray as $row)
{
$item           = $row['item'];
$description    = $row['description'];
$rate           = $row['rate'];
$qty            = $row['qty'];
$price          = $row['price'];
$itemid         = $row['itemid'];

if($row['itemid'] >= 1)
{
    $query = "UPDATE `invoice_items` SET `item` = '?', `description` = '?', `rate` = '?', `qty` = '?', `price` = '?' WHERE `id` = '?' LIMIT 1;";
    $query_prepare = $this->prep_q($query,array($item,$description,$rate,$qty,$price,$itemid));
}
else
{
    $query = "INSERT INTO `invoice_items` (`item`,`invoice_id`,`description`,`rate`,`qty`,`price`) VALUES ('?','?','?','?','?','?') LIMIT 1;";
    $query_prepare = $this->prep_q($query,array($item,$invoice_id,$description,$rate,$qty,$price));
}

$this->setquery($query_prepare);
}
  • 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-08T04:07:28+00:00Added an answer on June 8, 2026 at 4:07 am

    Also you should create a workable array, each row has its own name, description, rate, itemid.

    So instead of:

    Array
    (
    [item] => Array
        (
            [0] => item listing 1
            [1] => item listing 2
        )
    
    [description] => Array
        (
            [0] => item testing description
            [1] => item testing description
        )
    
    [rate] => Array
        (
            [0] => 1.00
            [1] => 2.00
        )
    
    [itemid] => Array
        (
            [0] => 1
            [1] => 2
        )
    )
    

    Your array should look like:

    Array
    (
    [0] => Array
    (
            [item] => item listing 1
            [description] => item testing description
            [rate] => 1.00
            [itemid] => 1
    )
    [1] => Array
    (
            [item] => item listing 2
            [description] => item testing description
            [rate] => 2.00
            [itemid] => 2
    )
    

    Then you can simply use foreach, and PDO or mysqli:

    $sql['update'] = "UPDATE `invoice_items`
                      SET `item` = :item, `description` = :description, `rate` = :rate
                      WHERE `itemid` = :itemid";
    
    $sql['insert'] = "INSERT INTO `invoice_items` (itemid,item,description,rate)
                      VALUES (:itemid, :item, :description,:rate)";
    
    foreach ($array as $row){
        if($row['itemid'] >= 1){
            $stmt = $db->prepare($sql['update']);
        }else{
            $stmt = $db->prepare($sql['insert']);
        }
        $stmt->execute($row);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Borderline ServerFault question, but figured I'd try here first since I've had luck with
I had asked a question about this earlier, but it didn't get answered right
Being quite a newbie, I've just had my first question answered ( Delphi: TImage.Create
I had this question... answered and very nice it was too. But, oddity has
First question here and can I start by saying how much help this site
I'm working on my first programming assignment for Java and I had another question.
I recently had my app certified (first time!) but not automatically published. The game
This seems incredibly similar to a question I had answered just a few days
Greeting to the community. This is my very first question. I had in mind
Hey first question I'm asking here many thanks in advance. I'm using a vector

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.