0

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?

5
  • 2
    Where is your Line 60? Commented Jun 2, 2020 at 2:42
  • 1
    Is the notice typed or copy and pasted? Maybe $row['milage'] is the line and it should be mileage? Please clarify question. Also HTML here looks verrry old. Commented Jun 2, 2020 at 3:06
  • 1
    @HarishST lines 59 and 60 would be $milage = $_POST['mileage']; print $mileage; @user3783243 I did have that messed up yes fixed it same error as far as the html I'm not sure what needs to change as that's all I know of it Commented Jun 2, 2020 at 12:27
  • Is this solved? There are a few answers below. Commented Jun 3, 2020 at 3:40
  • @kelvin yes it is I am just not able to accept my own answer. Ended up being a few spelling mistakes and needing to assign ownership of the DB to Apache, well more specifically www-data Commented Jun 3, 2020 at 15:34

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

9 Comments

That worked but now I get Notice: Array to string conversion in /var/www/html/Oil/cars.php on line 71 Prepare failed: (Array) 00000 :/
Can you mention the Code in Line 71?
70 and 71 if (!($results = $db2->prepare($sql))){ echo "Prepare failed: (" . $db->errorInfo() . ") " . $db->errorCode();
Try echo "Prepare failed: "; print_r($db->errorInfo()); print_r($db->errorCode()); instead of echo "Prepare failed: (" . $db->errorInfo() . ") " . $db->errorCode();
Also not the issue. Again was my spelling chageDate should be changeDate
|
0

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.

4 Comments

So would it look like: <input type="Text" name="mileage" value='Text'/>?
yes, and then $_POST['mileage'] will equal 'Text'
Sadly that was not the problem. Slight spelling on my end
@Ron No. It will not cause the Notice Undefined index. It will print a string of length 0 ie, ''.
0

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

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.