0

I'm trying to detect if the data in the db is expired or not, and it works prefectly fine as long as I only have on data to check when there are mutiple data needed to be check then everything will somehow follow the first one's behavior

while($row=mysqli_fetch_assoc($exe))
{   
    date_default_timezone_set("Asia/Taipei");
    echo $row['ExpeTime']." ";
    echo $ExpTim=strtotime($row['ExpeTime']);
    echo " ";
    echo $TWnowSec=date("U");
    echo " ";

    /*if($ExpTim>$TWnowSec){
        echo "沒過期";
    }elseif($ExpTim<$TWnowSec){
        echo "過期了";
    }*/

    if($ExpTim<$TWnowSec) {
        $sql= "UPDATE productneeds SET Status='已過期';";
        if(!mysqli_stmt_prepare($stmt, $sql)){
            echo "update failed";
        }else{
            mysqli_stmt_execute($stmt);
            echo "sucess";
        }
    }else {
        echo "{$row['O_no']} is not expired<br>";
    }
}
2
  • 2
    Your SQL statement "UPDATE productneeds SET Status='已過期';" does not have a WHERE part and does therefor update ALL rows in the productneeds, not only one row which might be what you expect? Commented Apr 21, 2020 at 13:01
  • Your prepared/execute handling is not quite correct. See docs where you assign $stmt to prepare call whose first argument should be connection object, $conn. Then you execute the query using $stmt. Commented Apr 21, 2020 at 14:18

1 Answer 1

1

Consider a pure SQL update query and avoid any PHP looping. Since you appear to be looping from a different recordset fetch, run MySQL's supported UPDATE...JOIN query to update records across one or more tables. Some adjustments required to achieve your needed time zone. Below is a demonstration for you to finalize:

// SET MYSQL SESSION TIME ZONE TO GMT
$sql = "SET TIME_ZONE = '+00:00';"        
mysqli_query($conn, $sql);

// RUN UPDATE + JOIN WITH ADJUSTMENT FOR 'Asia/Taipei' TIME ZONE
$sql = "UPDATE productneeds p
        INNER JOIN myotherTable t
           ON p.myRelatedID = t.myRelatedID
        SET p.Status = '已過期' 
        WHERE t.ExpeTime < NOW() + INTERVAL 8 HOUR"; 

mysqli_query($conn, $sql);
Sign up to request clarification or add additional context in comments.

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.