Adamhumbug Posted August 23, 2023 Share Posted August 23, 2023 (edited) 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 August 23, 2023 by Adamhumbug Quote Link to comment Share on other sites More sharing options...
Barand Posted August 23, 2023 Share Posted August 23, 2023 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 Quote Link to comment Share on other sites More sharing options...
Solution maxxd Posted August 24, 2023 Solution Share Posted August 24, 2023 $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. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted August 24, 2023 Share Posted August 24, 2023 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) Quote Link to comment Share on other sites More sharing options...
Adamhumbug Posted August 24, 2023 Author Share Posted August 24, 2023 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted August 24, 2023 Share Posted August 24, 2023 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). Quote Link to comment Share on other sites More sharing options...
Adamhumbug Posted August 24, 2023 Author Share Posted August 24, 2023 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.