0

I have got the following code:

if (count($sales) > 0) {
    foreach ($sales as $sale) {
        //Generate variables
        $user_id = $_SESSION['user_id'];
        $sales_id = $sale['sales_id'];
        $product = $sale['product'];
        $quantity = $sale['quantity'];
        $default_printed = 0;
        $datetime = $_SESSION['session_date']." ".date('G:i:s');

        echo $sales_id.", ".$product.", ".$quantity.", ".$user_id."<br />";

        if ($stmt = $mysqli->prepare("INSERT INTO sales (uid,product,printed,saledatetime,jpgID,quantity) VALUES (?,?,?,?,?,?)")) {
            $stmt->bind_param('isisii',$user_id,$product,$default_printed,$datetime,$sales_id,$quantity);
            $stmt->execute();
            $stmt->close();
        }
    }
}

It should work, but for some reason it only adds to the database the FIRST time it runs... the echo produces the following, just to prove that the array is actually being looped through.

83, KR, 2, 2
84, KR, 1, 2
84, LR, 1, 2
85, KR, 1, 2
86, LR, 2, 2
87, KR, 1, 2
87, LR, 3, 2
89, KR, 2, 2

Why are the values not being added to the database on subsequent runs?

1
  • What exactly is your question? How does the desired output look like? Commented Jan 30, 2014 at 22:52

3 Answers 3

1

Maybe the subsequent insert statements fail because a database constraint prevents the data from being inserted. Have you tried executing the statements "by hand" one after the other?

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

Comments

1

Do not close the statement and connection inside the loop, close it after all the executions are done. Also you do not really need to 'prepare' the statement inside the loop. The whole idea is that you prepare() the statement outside the loop, then inside the loop you only pass values to execute() and once loop is completed, you close() the loop:

if (count($sales) > 0) {
    foreach ($sales as $sale) {
        //Generate variables
        $user_id = $_SESSION['user_id'];
        $sales_id = $sale['sales_id'];
        $product = $sale['product'];
        $quantity = $sale['quantity'];
        $default_printed = 0;
        $datetime = $_SESSION['session_date']." ".date('G:i:s');

        echo $sales_id.", ".$product.", ".$quantity.", ".$user_id."<br />";

        if ($stmt = $mysqli->prepare("INSERT INTO sales (uid,product,printed,saledatetime,jpgID,quantity) VALUES (?,?,?,?,?,?)")) {
            $stmt->bind_param('isisii',$user_id,$product,$default_printed,$datetime,$sales_id,$quantity);
            $stmt->execute();

        }
    }
            $stmt->close(); //move close() here
}

Comments

0

Just checked the database and I must of forgot to change the ID (Primary Key) to AUTO_INCREMENT so it was just being overwritten.

Cheers guys!

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.