简体   繁体   中英

Greater Than by a set number

Good afternoon, i've looked at a lot of things and can't seem to figure something out.

I am writing a program where a user inputs their vehicles mileage. Once that mileage is +3k, i want it to alert it's time for service, etc.

I am not sure how to accomplish this, if anyone can point me in a direction.

if reg_mileage = 35k, update_mileage = 38k, alert() is the idea.

Thank you!

if(isset($_POST['check_mileage'])) {
    
    $update_mileage = "UPDATE service_tracking SET update_mileage = '$reg_mileage' WHERE reg_vin = '$reg_vin'";
    $conn->exec($update_mileage);
    $check = $conn->query("SELECT reg_mileage, update_mileage FROM service_tracking WHERE (reg_mileage < update_mileage)");
    
    while($row = $check->fetch(PDO::FETCH_ASSOC)) {
        
        if($row > 38000) {
            echo "Time for an oil change";
        } else {
            echo "Up to date";
        }
    }
    
}

$row is an array, you can't compare it with a number. I think you want to subtract reg_mileage from update_mileage , and alert if the difference is less than 3K.

while ($row = $check->fetch(PDO::FETCH_ASSOC)) {
    if ($row['update_mileage'] - $row['check_mileage'] < 3000) {
        echo "Time for an oil change<br>";
    } else {
        echo "UP to date<br>";
    }
}

BTW, you probably should include the VIN or some other identifying information in the SELECT and output. Otherwise the user won't know which car needs the oil change.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM