0

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>';
    }
}

?>
Tibs
  • 735
  • 5
  • 16
Wen Qing
  • 109
  • 6
  • Could you explain your code and put only the part that is the problem? With all this code and no much explanation it's difficult. Also, your code is using MySQL so it's difficult to reproduce. – Anthony Jan 29 '18 at 19:49
  • @AnthonyB Hi i have edited already and give a brief explanation of what went wrong, tell me if you still do not get it – Wen Qing Jan 29 '18 at 19:57
  • What happens if you `var_dump($cardfrom);` and `var_dump($cardto);`? – WOUNDEDStevenJones Jan 29 '18 at 20:00
  • Side-remark: you don't need to connect and prepare multiple times. See this [answer](https://stackoverflow.com/a/19107074/1022914). – Mikey Jan 29 '18 at 20:00
  • @WOUNDEDStevenJones i will get 1001 for $cardfrom, and $cardto will be 1005, i believe i successful brought in both cardfrom and cardto, its just it only loop once, which is 1001 and 1002, but not 1003, 1004, 1005 – Wen Qing Jan 29 '18 at 20:02

1 Answers1

1

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

Akhilesh B Chandran
  • 6,523
  • 7
  • 28
  • 55