1

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";
        }
    }
    
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
tobdesired
  • 11
  • 1
  • 1
    Welcome to Stack Overflow! Your script is vulnerable to [SQL Injection Attack](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). Even if [you are escaping variables, its not safe](https://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string%5D)! You should always use [prepared statements and parameterized queries](https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php) in either MYSQLI or PDO instead of concatenating variables into the query. – Barmar Dec 27 '22 at 21:48

1 Answers1

2

$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.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • I do have it selected by vin, thank you for making sure. I've tried that code every which way and no matter what it seems like if it's less than 3k as a whole, it doesn't display the correct message. For instance if I do update_mileage = 10k + check_mileage = 1k, should display up to date, but doesn't. If I change it to like 5, it shows the appropriate message. Not sure why. – tobdesired Dec 28 '22 at 04:37
  • Maybe I misunderstood what you're trying to do. Can you add some sample data and the expected result to the question? – Barmar Dec 28 '22 at 15:08
  • I would like people to register their vin and mileage. Let's say they are curious if they need pm maintenance. They type in their vin and current mileage. I want the updated_mileage to compare to their reg_mileage. If it's >= 3k display they need maint. If it's < then 3k, display up to date. Then I want to start checking against their last input. – tobdesired Dec 29 '22 at 03:32
  • I thought you wanted to know when they were getting close to the mileage needed to check the oil, not 3k past it. I've reversed the comparison. – Barmar Dec 29 '22 at 15:41