循环

时间:2016-12-07 00:20:36

标签: php mysql while-loop

我有一个程序,你可以在其中设置每日目标并将其除以6,因为它将每4小时更新一次并添加每4小时划分的值。但是当我试图将查询结果放在循环中时,它运行不正常。当我试图将它放入数组并为它创建一个循环然后更新但程序进程很慢。任何有助于我的计划工作的帮助。感谢

<?php

                    if(isset($_POST['clicked'])){

                    $mysqli = new mysqli('10.237.2.152','root','c0k3float','monitoring');

                    $datess = date("Y-m-d H:i:s");

                     // echo "<script> alert('".$datess."'); </script>";
                    $timeplan = $_POST['submitTime'];

                    $a = array();

                    $results=$mysqli->query("SELECT * from Shipment_Target") or mysqli0;

                    while($row = $results->fetch_array())  {
                        $temp = $row['twohrs_target'];
                        $id = $row['id'];   
                        if ($timeplan =='6:00 AM-10:00 AM'){
                            $temp*=1;

                             $results = $mysqli->query("UPDATE Shipment_Target SET target=".$temp." where id=".$id." ");
                        }
                        else if ($timeplan == '6:00 AM-10:00 AM'){
                            $temp*=2;

                        }
                        else if ($timeplan == '10:00 AM-2:00 PM'){
                            $temp*=3;

                        }

                        else if ($timeplan =='2:00 PM-6:00 PM'){
                            $temp*=4;
                            //array_push($a,$temp);
                        }





                    }
                    // for($x=0; $X<=count($a); $x++){


                    // }
                    echo "<script> alert(".$res = count($a).");</script>";

                    echo "<script> window.location='shipment.php' </script>";

                    // $results = $mysqli->query("UPDATE Shipment_Target SET time='".$_POST['submitTime']."',target=".$temp.", date='".$datess."' ") or mysqli0;





                    }

                    ?>

1 个答案:

答案 0 :(得分:0)

您在外观中覆盖了$result变量。

// Here $result is the query SELECT
$results=$mysqli->query("SELECT * from Shipment_Target") or mysqli0;
while($row = $results->fetch_array())  {
    $temp = $row['twohrs_target'];
    $id = $row['id'];   
    if ($timeplan =='6:00 AM-10:00 AM'){
        $temp*=1;

        // Here $result is the query UPDATE
         $results = $mysqli->query("UPDATE Shipment_Target SET target=".$temp." where id=".$id." ");
    }

您可以使用其他变量名称,但不要使用相同的变量。