Wen Qing
Wen Qing

Reputation: 111

For Loop is not looping the numbers I want

I am trying to use for loop as a method to send in multiple data, however, the number that I want to loop is not the one I want to send in. For example, when i allocated 10001 - 10005, it only stores in the data 10001 - 10002. And everytime no matter how many different amount i allocate, it only stores in 2 data which is the first 2 number from the range i want to allocate, instead of the amount i required. I believe is the method of me looping got error. Please help.

@processallocate.php

See as Text

<?php

for ($cardfrom = $cardfrom; $cardfrom <= $cardto; $cardfrom++) {
    if ($cardfrom) {
        $mysqli = new mysqli(spf, dbuser, dbpw, db);
        $stmt = $mysqli->prepare("INSERT INTO allocation 
(cardfrom) 
VALUES (?)");
        $stmt->bind_param("s", $cardfrom);
        $result = $stmt->execute();
        if ($result == true && $stmt->affected_rows > 0) {
            echo "<b>" . $check . "</b><br />";
        }
        else {
            echo '<p>Taxi Voucher has already been allocated</p>';
            break;
        }

        $stmt->close();
        $mysqli->close();
    }
    else {
        echo '<p>Error</p>';
        echo '<p>Please <a href="allocation.php">Click Here</a> to resubmit</p>';
    }
}

?>

Upvotes: 0

Views: 85

Answers (1)

Akhilesh B Chandran
Akhilesh B Chandran

Reputation: 6608

I think the issue is with your IF..ELSE condition. You are using $cardfrom as the iteration counter.

And then inside the loop, you are checking whether the $cardfrom evaluates to any "truthy" value (a boolean TRUE, or a non-empty, non-NULL value, or non-zero number)

Only if its true, you are doing the mysql insertion part!

So please check the logic in your code and see if its meets your expectation!

Hope it helps

Upvotes: 1

Related Questions