Pittsie
Pittsie

Reputation: 79

Update SQL from PHP with textbox and button

I am simply trying to create a home webpage to where we can enter the mileage hit update which grabs the vehicle, and current date then pushes it to the DB.

The code I have below displays correctly other than maybe formatting the number to have a , but that wouldn't be a necessity.

When I hit update I get Notice: Undefined variable: mileage in /var/www/html/Oil/cars.php on line 60

<link href="status.css" rel="stylesheet">
<body bgcolor = #000000>
<font color = "white">

<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
 <META HTTP-EQUIV="refresh" CONTENT="86400"><style>
        body { 
            animation: fadeInAnimation ease 3s; 
            animation-iteration-count: 1; 
            animation-fill-mode: forwards; 
        } 
        @keyframes fadeInAnimation { 
            0% { 
                opacity: 0; 
            } 
            100% { 
                opacity: 1; 
            } 
        } 
    </style>
</head>

<center>
<font color = "red">




<?php
try {

    $db = new PDO("sqlite:/var/www/html/Oil/Vehicles.db");
    print "<center><table border=10 bordercolorlight=#383838 bordercolordark=Gray>";
    print "<tr><td class='toprow'><u>Vehicle</u></td><td class='toprow'><u>Mileage</u></td><td class='toprow'><u>Filter Size</u></td><td class='toprow'><u>Oil Capacity</u></td><td class='toprow'><u>Oil Type</u></td><td class='toprow'><u>Change Date</u></td><td class='toprow'><u>New Miles</u></td><td class='toprow'><u>Update</u></td></tr>";
    $result2 = $db->query('SELECT * FROM Cars');

    foreach ($result2 as $row) {

        print '<tr><td>' . $row['vehicle'] .'</td>';
        print "<td>" . $row['mileage'] . "</td>";
        print '<td>' . $row['filter'] .'</td>';
        print '<td>' . $row['oilCap'] .'</td>';
        print '<td>' . $row['oilType'] .'</td>';
        print '<td>' . $row['changeDate'] .'</td>';
    print '<td><form action="" method="POST"><input type="Text" name= "mileage"/><input type="hidden" name="vehicle" value="' .$row['vehicle']. '"/></td>';
    print '<td><button class="btn btn-default" type="submit" name="update">Update</button></form></td></tr>';
    }
    print "</table></center>";

} catch (PDOException $e) {
    echo $e->getMessage();
}
if (isset($_POST['update'])) {
    ini_set('display_errors', 1);
    error_reporting(E_ALL);

    $mileage = $_POST['mileage'];
    print $mileage;
    $vehicle = $_POST['vehicle'];
    print $vehicle;
    $change = date("m/d/Y");
    print $change;

    $sql = "UPDATE cars SET mileage = :mileage, chageDate = :change WHERE vehicle = :vehicle";

    try{
            $db2 = new PDO("sqlite:/var/www/html/Oil/Vehicles.db");
        if (!($results = $db2->prepare($sql))){
            echo "Prepare failed: (" . $db->errorInfo() . ") " . $db->errorCode();
        }elseif (!$results->bindValue(':mileage', $mileage)){
            echo "Binding parameters failed: (" . $results->errorInfo() . ") " . $results->errorCode();
        }elseif (!$results->bindValue(':change', $change)){
            echo "Binding parameters failed: (" . $results->errorInfo() . ") " . $results->errorCode();
        }elseif (!$results->bindValue(':vehicle', $vehicle)){
            echo "Binding parameters failed: (" . $results->errorInfo() . ") " . $results->errorCode();
        }elseif (!$results->execute()) {
            echo "Execute failed:\n";
            print_r($results->errorInfo());
        }else{
            $count = $results ->rowCount();
            if($results ->rowCount() == 0)
                echo 'Error';
            else
                echo 'Changed!';
        }

        } catch (PDOException $e) {
            echo $e->getMessage();
        }

}

?>

Maybe I'm not passing mileage correctly?

Upvotes: 0

Views: 95

Answers (3)

Pittsie
Pittsie

Reputation: 79

Ended up being 2 spelling issues on my part and then ran into a permission error on the DB. After fixing all it worked as intended

Upvotes: 0

Harish ST
Harish ST

Reputation: 1503

In your code,

The line 59 and 60 as you said are:

$milage = $_POST['mileage'];
print $mileage;

You are assigning the value from form field to $milage, but you are trying to print a variable named $mileage. Check the spelling. They are different. So the variable you are trying to print is obviously undefined.

Change the code to,

$mileage= $_POST['mileage'];
print $mileage;

Eventhough, it's just a typo. It can cause lot of debug time. So, try to understand the error and the problem will be somewhere beside it or it will have some relation atleast.

Upvotes: 1

Ron
Ron

Reputation: 6551

You are not providing an actual value='' in <input type="Text" name= "mileage"/>, so when Submitting $_POST['mileage']; is empty, and you get the notice.

Upvotes: 0

Related Questions