connection.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "carDB";
try {
$conn = new PDO("mysql:host=$servername;dbname=$database", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>
addform.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Add Record</title>
</head>
<body>
<h1>Add New Car Record</h1>
<form action="processform.php" method="POST">
Car Manufacturer : <input type="text" name="manufacturer" required> <br>
Car Model : <input type="text" name="model" required> <br>
<input type="submit" value="Save Record">
</form>
</body>
</html>
processform.php
<?php
include('connection.php');
$manufacturer = $_POST['manufacturer'];
$model = $_POST['model'];
$sql = "INSERT INTO cars (manufacturer, model) values (?,?)";
$stmt = $conn->prepare($sql);
$stmt->execute(array($manufacturer,$model));
header('location:viewcars.php');
?>
viewcars.php
<?php
// include the connection
include('connection.php');
// SQL Command for displaying records
$sql = "SELECT * from cars";
// get the information
$stmt = $conn->query($sql);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
$totalrec = $stmt->rowCount();
if ($totalrec > 0) {
echo "<table border=1>";
echo "<tr>";
echo "<th> Manufacturer</th>";
echo "<th> Model </th>";
echo "<th> Action </th>";
echo "</tr>";
foreach($rows as $row) {
echo "<tr>";
echo "<td> {$row['manufacturer']} </td>";
echo "<td> {$row['model']} </td>";
echo "<td> <a href=delete.php?car_id={$row['car_id']}>Delete</a> |
<a href=edit.php?car_id={$row['car_id']}>
Edit </a></td>";
echo "</tr>";
}
echo "</table>";
} else {
echo "<h1>No records found</h1>";
}
?>
Delete Record
<?php
include('connection.php');
$car_id = $_GET['car_id'];
$sql = "DELETE FROM `cars` WHERE `cars`.`car_id` = $car_id ";
$stmt=$conn->query($sql);
$stmt->execute();
header('location: viewcars.php');
?>
Edit Record
<?php
include('connection.php');
$manufacturer="";
$model="";
if (isset($_GET['car_id'])) {
$car_id = $_GET['car_id'];
$sql = "SELECT * from cars where car_id = $car_id";
$stmt = $conn->query($sql);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$manufacturer = $row['manufacturer'];
$model = $row['model'];
$car_id = $row['car_id'];
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Edit Record</title>
</head>
<body>
<h1>Edit Car Record</h1>
<form action="update.php" method="POST">
Car Manufacturer :
<input type="text" name="manufacturer" required value="<?php
if (!empty($manufacturer)) { echo $manufacturer; } else { echo ''; }
?>"> <br>
Car Model :
<input type="text" name="model" required
value ="<?php
if (!empty($model)) {
echo $model;
} else {
echo '';
}
?>"
> <br>
<input type="hidden" name="car_id" value="<?php echo $car_id; ?>">
<input type="submit" value="Update Record">
</form>
</body>
</html>
Update Record
<?php
include('connection.php');
$manufacturer = $_POST['manufacturer'];
$model = $_POST['model'];
$car_id = $_POST['car_id'];
$sql = "UPDATE cars SET model = ?, manufacturer = ? WHERE car_id = $car_id";
$stmt = $conn->prepare($sql);
$stmt->execute(array($model,$manufacturer));
header('location: viewcars.php');
?>