Jump to content

insert subquery


neridaj

Recommended Posts

Hello,

 

I'm trying to do an insert that contains a few subqueries to update some invoice amounts but my query isn't working and I think there might be a better way to do this like maybe a stored procedure or function. I'm sure there is a more elegant way to do this so if you have any ideas please let me know.

 

function update_invoice($invnum, $paid)
{
  // update existing invoice
  $conn = db_connect();

  // add current amount paid to new amount paid and subtract from balance
  if(!$conn->query("UPDATE invoice SET inv_amt_paid = (SELECT inv_amt_paid FROM invoice WHERE inv_number = $invnum) + $paid, inv_amt_balance = (SELECT inv_amt_balance FROM invoice WHERE inv_number = $invnum) - (SELECT inv_amt_paid FROM invoice WHERE inv_number = $invnum) WHERE inv_number = $invnum);"))
    throw new Exception('Invoice could not be updated.');

  return true;
}

 

Thanks,

 

J

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.