-3

I'm working on a webpage where I allow users to edit their car information. In the mainlining, there is an edit button (input - type text with a hidden key value) where it takes the user to this "edit car info" page. Initially, once the page is opened for the first time, this hidden value is used to query the database, retrieve original information and and set them as placeholders for the field. The user can write information in the input field then press the "submit edit" button which then updates the row in the database table. However, I get an error that the name of the hidden value is undefined. I don't understand how it can be undefined for the update query when it was working just fine for the select query. Can anyone shed a light on this? What should I do? This is a picture of the errors:

This is a picture of the errors

This is the mainlanding code: (hidden value is set here)

<?php
$mysqli= new mysqli("localhost", "root","","Car_registration"); 
if(empty($_SESSION)) // if the session not yet started
session_start();
if(isset($_SESSION['username'])) { // if user already logged in
header("location: mainlanding_user.php"); //send to homepage
exit;
}
?> 

<!DOCTYPE html>

<html>

<head>
  <title> Car Registration: User's Mainlanding </title>
  <link href="css/style3.css" rel="stylesheet">
</head>

<body>

<header> 

<h1>Account Information</h1>
<img id="img1" src= "image/car.jpg" alt ="car image">

</header> 

<nav id='nav'>
<form action="logout.php">
<input type="submit" value="  Logout " id="button">
</form>
</nav>


<h2>Profile </h2>

<div class='container1'>

<?php 

$username="root";
$password="";
$database="Car_registration";

$mysqli= new mysqli("localhost",$username,$password,$database); 


$query= "select * from driver where username='".$_SESSION['logged_username']."'";
$result = $mysqli->query($query);


while( $row = $result->fetch_assoc() ){

echo "<div id='container'>" ;
echo "<dl> <dt>First Name</dt> <dd>".$row['Fname'];
echo "</dd> <br> <dt>Last name</dt><dd>".$row['Lname'];
echo "</dd> <br> <dt>License Number</dt><dd>".$row['license_no'];
echo "</dd> <br> <dt>Age</dt><dd>".$row['Age'];
echo "</dd> <br> <dt>Birthday</dt><dd>".$row['bdate'];
echo "</dd> <br> <dt>City</dt><dd>".$row['City'];
echo "</dd></dl>";
echo "</div>";

$license_no = $row['license_no']; //used for finding cars

}

?>


<div class="align-me">
  <div class="form-wrapper" action="search_plate_no.php">
    <form class="center">
      <input class="input-fields" name="search" type="text" placeholder="Search a plate number">
      <input class="input-fields submit" name="find" type="submit" value="Search">
    </form>
  </div>
</div>






<h3> Registered Cars </h3>

<div class='container2'>

<?php 

$username="root";
$password="";
$database="Car_registration";

$mysqli= new mysqli("localhost",$username,$password,$database); 


$query= "select * from cars where license_no='".$license_no."'";
$result = $mysqli->query($query);


echo "<table border=1>
<tr>
<th>Plate No.</th>
<th>License No.</th>
<th>Car Type</th>
<th>Fines</th>
<th>City</th>
<th>Edit</th>
<th>Delete</th>
</tr>";


while ($temp = $result->fetch_assoc()){
?> 

<tr>
    <td><?php echo $temp['Plate_no']; ?></td>
    <td><?php echo $temp['license_no']; ?></td>
    <td><?php echo $temp['Car_type']; ?></td>   
    <td><?php echo $temp['Fines']; ?></td>
    <td><?php echo $temp['city']; ?></td>         
   <td>
   <form action = "edit_car.php" method="post">
      <input type="hidden" name="id" value="<?php echo $temp['Plate_no']; ?>">
      <input type="submit" name="edit" value="Edit">
   </form>
</td>

<td>
   <form action = "delete_car.php" method="post">
      <input type="hidden" name="id" value="<?php echo $temp['Plate_no']; ?>">
      <input type="submit" name="delete" value="Delete">
   </form>
</td>
    
  </tr> 
<?php
}
?>
</table>

</div>

 
<form action="register_car.php">
<input type="submit" value="  Register Car " id="button2">
</form>

<footer> 
<h4> All rights belong to Car Registration Inc. </h4>
<img id="img3" src= "image/license.png" alt ="license plates image">

</footer>


</body>

</html>

Edit car page: (Error is generated here)

 <!DOCTYPE html>
<html>

<head>
  <title> Edit Car Information Page </title>
  <link href="css/style2.css" rel="stylesheet">
   
</head>

<body>

<div class="container">

<header> 
<h1>Edit Car Information </h1>
<img id="img1" src= "image/register.png" alt ="Registration image">
</header>  

<?php 

$username="root";
$password="";
$database="Car_registration";

$mysqli= new mysqli("localhost",$username,$password,$database); 

$plate_no= $_POST["id"]; //This line causes an error 
$_SESSION['plateNo'] = $plate_no; 

$query= "select * from cars where Plate_no='".$plate_no."'";
$result = $mysqli->query($query);  

while( $row = $result->fetch_assoc()){

$plate_no = $row['Plate_no']; 
$car_type = $row['Car_type']; 
}

?>

<main>
<h2> You can only edit the following information: </h2>

<form action="" method="post">

<label for="car_type_input">Car Type:</label>
<input type="text" placeholder="<?php echo $car_type?>" id="car_type_input" name="car_type_input"><br><br>


<div class="vertical-center">
<input type="submit" value="  Submit Edit "  name="button1" id="button1">
</div>

</form>

<?php 

$username="root";
$password="";
$database="Car_registration";

$mysqli= new mysqli("localhost",$username,$password,$database); 

if( isset($_POST['button1']) ){  //If user changed field, take value. If not, keep old value. 

if( !empty($_POST['car_type_input']) ){ //If there is user input 
$car_type_2 = $_POST['car_type_input']; 
$query= "update cars set Car_type='".$car_type_2."' WHERE Plate_no='".$_SESSION['plateNo']."'";
}


if ($mysqli->query($query))
echo "Fields updated successfuly!";
else 
echo "Update Fields Failed!";

}

?>


</main>
<footer> 
<h3> All rights belong to Car Registration Inc. </h3>
<img id="img3" src= "image/license.png" alt ="license plates image">

</footer>

</div>
</body>

</html>
Marcinek
  • 2,144
  • 1
  • 19
  • 25
lll
  • 1
  • 6

3 Answers3

-1
Use $plate_no= $_POST['id']; instead of $plate_no= $_POST["id"];
B_Sharp
  • 11
  • 3
-1

Here why you close the while loop ??

while ($temp = $result->fetch_assoc()){
?> 

and here too

<?php
}

Try this:

print"<h3> Registered Cars </h3>

<div class='container2'>";


$username="root";
$password="";
$database="Car_registration";

$mysqli= new mysqli("localhost",$username,$password,$database); 


$query= "select * from cars where license_no='".$license_no."'";
$result = $mysqli->query($query);


echo "<table border=1>
<tr>
<th>Plate No.</th>
<th>License No.</th>
<th>Car Type</th>
<th>Fines</th>
<th>City</th>
<th>Edit</th>
<th>Delete</th>
</tr>";


while ($temp = $result->fetch_assoc())
{

print"
<tr>
    <td><?php echo $temp['Plate_no']; ?></td>
    <td><?php echo $temp['license_no']; ?></td>
    <td><?php echo $temp['Car_type']; ?></td>   
    <td><?php echo $temp['Fines']; ?></td>
    <td><?php echo $temp['city']; ?></td>         
   <td>
   <form action = "edit_car.php" method="post">
      <input type="hidden" name="id" value="<?php echo $temp['Plate_no']; ?>">
      <input type="submit" name="edit" value="Edit">
   </form>
</td>

<td>
   <form action = "delete_car.php" method="post">
      <input type="hidden" name="id" value="<?php echo $temp['Plate_no']; ?>">
      <input type="submit" name="delete" value="Delete">
   </form>
</td>
    
  </tr> ";

}
print"</table>

</div>";

B_Sharp
  • 11
  • 3
  • This is so I could print the table in html instead of echoing the html inside the php code. It works fine the way I do it but that you for the suggestion! – lll Nov 22 '20 at 13:21
-1

you are not sending id that's because error appears use this code to check if id exists first:

$plate_no='';
$car_type = '';
if(isset($_POST["id"])){
    $plate_no= $_POST["id"]; //This line causes an error 
    $_SESSION['plateNo'] = $plate_no; 

    $query= "select * from cars where Plate_no='".$plate_no."'";
    $result = $mysqli->query($query);  

    while( $row = $result->fetch_assoc()){
        $plate_no = $row['Plate_no']; 
        $car_type = $row['Car_type']; 
    }
}
Babak Asadzadeh
  • 1,207
  • 1
  • 11
  • 21
  • I will try this and let you know how it goes. What action should I take in case id isn't set? I was trying to send the plate_no as a session variable – lll Nov 22 '20 at 13:23
  • it is your decision you can validate that and make it required or do something else – Babak Asadzadeh Nov 22 '20 at 13:30