douhui4699 2018-01-29 19:43
浏览 30
已采纳

For Loop并没有循环我想要的数字

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

?>
  • 写回答

1条回答 默认 最新

  • dongzhouzhang8696 2018-01-29 20:02
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条