0

I am returning an array of results which I put into a foreach loop and each result checks against a single amount. As long as the condition is met it should insert rows. However, no data is ever inserted into the database but the error logs are also empty ie: I get no errors.

        foreach ($invoices as $invoice) {
           if ($invoice->amount <= $this->amount) {
               $stmt = $db->prepare("INSERT INTO `payment_link` (`amount`) VALUES (:amount)");
               $stmt->bindValue(":amount", $invoice->amount, PDO::PARAM_STR);
               $stmt->execute();
           }
        }

print_r($invoices) gives me:

Array ( [0] => App\Models\Invoice Object ( [customer_id] => [description] => [date_created] => [amount] => 300.00 [total:App\Models\Invoice:private] => [invoice_id] => [line_description] => [line_amount] => [errors] => Array ( ) ) [1] => App\Models\Invoice Object ( [customer_id] => [description] => [date_created] => [amount] => 500.00 [total:App\Models\Invoice:private] => [invoice_id] => [line_description] => [line_amount] => [errors] => Array ( ) ) ) Array ( [0] => App\Models\Invoice Object ( [customer_id] => [description] => [date_created] => [amount] => 300.00 [total:App\Models\Invoice:private] => [invoice_id] => [line_description] => [line_amount] => [errors] => Array ( ) ) [1] => App\Models\Invoice Object ( [customer_id] => [description] => [date_created] => [amount] => 500.00 [total:App\Models\Invoice:private] => [invoice_id] => [line_description] => [line_amount] => [errors] => Array ( ) ) )
19
  • check this if ($invoice->amount <= $this->amount) { condition, if its TRUE, second check print_r($invoices) what r u getting and share the result. Commented Mar 28, 2019 at 15:02
  • Thank you. I $this->amount is 800 and $invoice->amount are 300 and 500. If I echo out $invoice->amount then I get 300 and 500 so it is true. Will post the print_r in my original question now.. Commented Mar 28, 2019 at 15:06
  • 1
    replace VALUES (:amount) with VALUES (?) and try. Commented Mar 28, 2019 at 15:06
  • That gives me Invalid parameter number: parameter was not defined' Commented Mar 28, 2019 at 15:09
  • 2
    @executable That's mysqli syntax, OP is using PDO. Commented Mar 28, 2019 at 15:10

1 Answer 1

1

[Based on the code in the question and not discussion in the comments]

You need to get eyes-on; have the computer tell you why it hasn't inserted any rows. From the php doc: $stmt->execute(); Returns TRUE on success or FALSE on failure. .

Test the result and post it to the error log (or echo to screen, whatever works for you).

$result = $stmt->execute();
if ($result) {
    error_log("row inserted successfully");
} 
else {
    error_log("insert FAILED");
}

If there is no feedback, the loop didn't execute or one of the previous statements failed. (They also return TRUE on success and FALSE on failure).

If it says "insert FAILED", you can get more detailed information from PDOStatement::errorinfo.

If it says "rows inserted successfully", well then there is something to investigate. (For example: does program use TRANSACTIONS and needs a commit?)

Start with gathering the information you don't have, it will help you track down the real problem.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.