Jump to content

Stringing Queries Together


Adamhumbug
Go to solution Solved by maxxd,

Recommended Posts

HI All,

I have a php function.

function removeItemFromQuote($itemId)
{
    include 'includes/dbconn.php';
    $sql = "DELETE from quote_items where id = :itemId";
    $stmt = $pdo->prepare($sql);
    $stmt->execute([
        ":itemId" => $itemId
    ]);

    $sql2 = "SELECT id, amount_charged_each, quantity, chargable_units from quote_items where quote_id = :qId";
    $stmt2 = $pdo -> prepare($sql2);
    $stmt2 -> execute([
        ':id' => $_GET['quoteId']
    ]);

    $total = 0;
    while($row = $stmt2 -> fetch()){
        $total += floatval($row['amount_charged_each']) * floatval($row['quantity']) * floatval($row['chargable_units']);
    }

    $sql3 = "UPDATE quote SET total_value = :total_value where id = :id";
    $stmt3 = $pdo -> prepare($sql3);
    $stmt3 -> execute([
        ':total_value' => $total,
        ':id' => $_GET['quoteId']
    ]);


}

The first part is running, the item is being deleted.

The second and third dont seem to be working, certainly the total_value column is not being updated.

I am not seeing any errors and i have this at the top of the page calling the function and in my functions file.

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

Is there something obvious that i am doing wrong?

Edited by Adamhumbug
Link to comment
Share on other sites

49 minutes ago, Adamhumbug said:

Is there something obvious that i am doing wrong?

Several things.

Working backwards

  • Trying to set "display_startup_errors" in your code is a fool's errand. If you have a startup error then the script won't run. If it won't run, how is it going to set that value?
    Set those values in you php.ini file
  • Sql3 - Do not store derived values, like totals. These should be found by querying the stored values.
  • Sql2 - all you want is the total value of the items, so get that directly in the query. No need to get all the items then iterate through them.
  • Return the total from the function.
$res = $pdo->prepare("SELECT SUM(amount_charged_each * quantity * chargable_units) 
                      FROM quote_items 
                      WHERE quote_id = :qId
                     ");
$res->execute([ $_GET['quote_id'] ]);
return $res->fetchColumn();                 // return the total 

 

Link to comment
Share on other sites

  • Solution
    $sql2 = "SELECT id, amount_charged_each, quantity, chargable_units from quote_items where quote_id = :qId";
    $stmt2 = $pdo -> prepare($sql2);
    $stmt2 -> execute([
        ':id' => $_GET['quoteId']
    ]);

There's also a fairly obvious problem here in that place marker names need to match the index of the values.

Link to comment
Share on other sites

Something that nobody has mentioned.  If you want to do this work inside of a function you probably want to make it usable in multiple situations.  Why then do you make it not portable by referencing an outside value that is not part of the function's header?  That is just not good programming.  Get rid of the $_GET reference and pass this 2nd value in using the parms in the function header.

function removeItemFromQuote($itemId, $other_id)

 

Link to comment
Share on other sites

2 hours ago, ginerjm said:

Something that nobody has mentioned.  If you want to do this work inside of a function you probably want to make it usable in multiple situations.  Why then do you make it not portable by referencing an outside value that is not part of the function's header?  That is just not good programming.  Get rid of the $_GET reference and pass this 2nd value in using the parms in the function header.

function removeItemFromQuote($itemId, $other_id)

 

I have rewritten the function and done exactly this.  Closer looking and there were several problems with it. 

 

All of your pointers have got me to the bottom of it - thanks all.

Link to comment
Share on other sites

4 minutes ago, Barand said:

One more point, do not connect to your db server every time you call a function. Connecting is the slowest part of the process. Connect once at the start of the script and pass $pdo as an argument in the function calls (as with the other arguments that @ginerjm suggested).

So dont "include/includes..." in each function, but when calling the function use "callingFunction($pdo, $otherThing)"?

 

Ok sounds good.

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.