Table of Contents
db-config.php
<?php
// w3 MySQL Connect PDO
$servername = "localhost";
$username = "root";
$password = "";
$db_name = "northwind_t";
try {
$conn = new PDO("mysql:host=$servername;dbname=$db_name", $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();
}
?>
<?php
/*
05.5050-PHP-MySQL-CRUD-Simple
customer_list.php
Teeratus_R
Edit : 20230109
*/
session_start();
require_once "../../config/db-config.php";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="shortcut icon" href="../../webcomponents/favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="../../bootstrap/bootstrap.min.css">
<style>
thead {
background-color: #ffc;
}
</style>
</head>
<body>
<h4>Template Filename : customer_list.php</h4>
<p>05.5050-PHP-MySQL-CRUD-Simple</p>
<?php
print_r($_POST);
echo "<hr>";
print_r($_GET);
echo "<hr>";
print_r($_SESSION);
echo "<hr>";
?>
<?php
if (isset($_SESSION['error'])) {
?>
<div class="alert alert-danger" role="alert">
<?php
echo $_SESSION['error'];
unset($_SESSION['error']);
?>
</div>
<?php
}
?>
<?php
if (isset($_SESSION['success'])) {
?>
<div class="alert alert-success" role="alert">
<?php
echo $_SESSION['success'];
unset($_SESSION['success']);
?>
</div>
<?php
}
?>
<div class="d-flex gap-3 justify-content-start mb-3">
<a href="index.php" class="btn btn-primary">Home</a>
<a href="customer_new.php" class="btn btn-primary">New</a>
<a href="customer_list.php" class="btn btn-success">List</a>
<!-- <a href="customer_edit.php" class="btn btn-warning disabled">Edit</a> -->
</div>
<table class="table table-striped table-bordered table-hover">
<thead>
<th>CustomerCode1</th>
<th>CompanyName2</th>
<th>ContactName</th>
<th>Address</th>
<th>City</th>
<th>Action</th>
<th>CustomerID</th>
</thead>
<tbody>
<?php
$sql_customer =
"SELECT
CustomerID,
CustomerCode,
CompanyName,
ContactName,
Address,
City
FROM Customers
ORDER BY CustomerCode ASC
;";
$stmt = $conn->query($sql_customer);
$stmt->execute();
$customer_s = $stmt->fetchAll();
if (!$customer_s) {
echo "<tr><td colspan='7' class='text-center'>No data found</td></tr>";
} else {
foreach ($customer_s as $customer_data) {
?>
<tr>
<td><?=$customer_data['CustomerCode'];?></td>
<td><?=$customer_data['CompanyName'];?></td>
<td><?=$customer_data['ContactName'];?></td>
<td><?=$customer_data['Address'];?></td>
<td><?=$customer_data['City'];?></td>
<td><a href="./customer_details.php?CustomerID=<?=$customer_data['CustomerID'];?>" class="btn btn-warning">Details</a></td>
<td><?=$customer_data['CustomerID'];?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
<script src="../../bootstrap/bootstrap.bundle.min.js"></script>
</body>
</html>
customer_details.php
<?php
/*
customer_details.php
Teeratus_R
*/
session_start();
require_once "../../config/db-config.php";
$CustomerID = $_GET['CustomerID'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>N-Delete</title>
<link rel="shortcut icon" href="../../webcomponents/favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="../../bootstrap/bootstrap.min.css">
</head>
<body>
<div class="container">
<?php
print_r($_POST);
echo "<hr>";
print_r($_GET);
echo "<hr>";
print_r($_SESSION);
echo "<hr>";
$sql_cust =
"SELECT
CustomerCode,
CompanyName,
ContactName,
Address,
City
FROM Customers
WHERE CustomerID = $CustomerID
;";
$stmt = $conn->prepare($sql_cust);
$stmt->execute();
$Customer_data = $stmt->fetch(PDO::FETCH_ASSOC);
// $CustomerID = $Customer_data['CustomerID'];
$CustomerCode = $Customer_data['CustomerCode'];
$CompanyName = $Customer_data['CompanyName'];
$ContactName = $Customer_data['ContactName'];
// $x = $Customer_data['x'];
?>
<h2 class="text-danger">Delete Customer ?</h2>
<div class="d-flex gap-3 justify-content-start">
<!-- <a href="customer_list.php" class="btn btn-primary">List</a> -->
</div>
<div class="row">
<div class="border col-sm-6 ">
<form action="customer_delete_db.php" method="post">
<div class="mb-3">
<label for="CustomerCode" class="form-label">CustomerCode</label>
<input type="text" class="form-control" name="CustomerCode" value="<?=$CustomerCode?>">
</div>
<div class="mb-3">
<label for="CompanyName" class="form-label">CompanyName</label>
<input type="text" class="form-control" name="CompanyName" value="<?=$CompanyName?>">
</div>
<div class="mb-3">
<label for="ContactName" class="form-label">ContactName</label>
<input type="text" class="form-control" name="ContactName" value="<?=$ContactName?>">
</div>
<div class="mb-3">
<label for="CustomerID" class="form-label">CustomerID</label>
<input type="text" class="form-control" name="CustomerID" value="<?=$CustomerID?>">
</div>
<div class="d-flex gap-2 justify-content-start mb-3">
<button type="submit" class="btn btn-danger">Confirm</button>
<a href="./customer_details.php?CustomerID=<?=$CustomerID?>" class="btn btn-secondary">Cancel</a>
</div>
</form>
</div>
</div>
</div>
<script src="../../bootstrap/bootstrap.bundle.min.js"></script>
</body>
</html>
customer_new.php
<?php
/*
05.5050-PHP-MySQL-CRUD-Simple
customer_new.php
Teeratus_R
Edit : 20221229
*/
session_start();
require_once "../../config/db-config.php";
// $CustomerID = $_GET['CustomerID'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>N-Details</title>
<link rel="shortcut icon" href="../../webcomponents/favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="../../bootstrap/bootstrap.min.css">
</head>
<body>
<div class="container">
<h2>Template Filename : Create New Customer</h2>
<?php
print_r($_POST);
echo "<hr>";
print_r($_GET);
echo "<hr>";
print_r($_SESSION);
echo "<hr>";
?>
<div class="d-flex gap-3 justify-content-start">
<!-- <a href="customer_list.php" class="btn btn-primary">List</a> -->
</div>
<div class="row">
<div class="border col-sm-6 ">
<form action="customer_new_db.php" method="post">
<div class="mb-3">
<label for="CustomerCode" class="form-label">CustomerCode</label>
<input type="text" class="form-control" name="CustomerCode" required>
</div>
<div class="mb-3">
<label for="CompanyName" class="form-label">CompanyName</label>
<input type="text" class="form-control" name="CompanyName" required>
</div>
<div class="mb-3">
<label for="ContactName" class="form-label">ContactName</label>
<input type="text" class="form-control" name="ContactName">
</div>
<div class="d-flex gap-2 justify-content-end mb-3">
<button type="submit" class="btn btn-primary">Submit</button>
<a href="./customer_list.php" class="btn btn-secondary">Cancel</a>
</div>
</form>
</div>
<!-- class="row" -->
</div>
</div>
<script src="../../bootstrap/bootstrap.bundle.min.js"></script>
</body>
</html>
customer_new_db.php
<?php
/*
05.5050-PHP-MySQL-CRUD-Simple
customer_new_db.php
Teeratus_R
Edit : 20221229
*/
session_start();
require_once '../../config/db-config.php';
$CustomerCodeRaw = trim($_POST['CustomerCode']);
$CustomerCode = strtoupper($CustomerCodeRaw);
$CompanyName = $_POST['CompanyName'];
$ContactName = $_POST['ContactName'];
$Address = $_POST['Address'];
$City = $_POST['City'];
// $x = $_POST['x'];
try {
$sql_cust = "INSERT INTO Customers
(CustomerCode, CompanyName, ContactName)
VALUES
(
'$CustomerCode',
'$CompanyName',
'$ContactName'
-- '$Address',
-- '$City'
)";
$conn->exec($sql_cust);
$last_id = $conn->lastInsertId();
$conn = null;
$_SESSION['success'] = "Data has been updated succesfully";
// header("location: ./customer_list.php");
header("location: ./customer_details.php?CustomerID={$last_id}");
} catch(PDOException $e) {
// echo $sql . "<br>" . $e->getMessage();
$_SESSION['error'] = "Data has not been updated succesfully" . $e->getMessage();
header("location: ./customer_list.php");
}
?>
customer_edit.php
<?php
/*
05.5050-PHP-MySQL-CRUD-Simple
customer_edit.php
Teeratus_R
Edit : 20230109
*/
session_start();
require_once "../../config/db-config.php";
$CustomerID = $_GET['CustomerID'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>N-Details</title>
<link rel="shortcut icon" href="../../webcomponents/favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="../../bootstrap/bootstrap.min.css">
</head>
<body>
<div class="container">
<?php
print_r($_POST);
echo "<hr>";
print_r($_GET);
echo "<hr>";
print_r($_SESSION);
echo "<hr>";
?>
<h2>Edit Customer</h2>
<?php
$sql_customer =
"SELECT
CustomerID,
CustomerCode,
CompanyName,
ContactName,
Address,
City
FROM Customers
WHERE CustomerID = $CustomerID
;";
$stmt = $conn->query($sql_customer);
$stmt->execute();
$customer = $stmt->fetch();
$CustomerCode = $customer['CustomerCode'];
$CompanyName = $customer['CompanyName'];
$ContactName = $customer['ContactName'];
$Address = $customer['Address'];
$City = $customer['City'];
?>
<div class="d-flex gap-3 justify-content-start mb-3">
<a href="customer_list.php" class="btn btn-primary">List</a>
</div>
<div class="row">
<div class="border col-sm-6">
<form action="customer_edit_db.php" method="post">
<input type="hidden" name="CustomerID" value="<?=$CustomerID?>">
<div class="mb-3">
<label for="CustomerCode" class="form-label">CustomerCode</label>
<input type="text" class="form-control" name="CustomerCode" value="<?=$CustomerCode?>">
</div>
<div class="mb-3">
<label for="CompanyName" class="form-label">CompanyName</label>
<input type="text" class="form-control" name="CompanyName" value="<?=$CompanyName?>">
</div>
<div class="mb-3">
<label for="ContactName" class="form-label">ContactName</label>
<input type="text" class="form-control" name="ContactName" value="<?=$ContactName?>">
</div>
<div class="mb-3">
<label for="Address" class="form-label">Address</label>
<input type="text" class="form-control" name="Address" value="<?=$Address?>">
</div>
<div class="mb-3">
<label for="City" class="form-label">City</label>
<input type="text" class="form-control" name="City" value="<?=$City?>">
</div>
<div class="d-flex gap-2 justify-content-end mb-3">
<button type="submit" class="btn btn-primary">Submit</button>
<a href="./customer_details.php?CustomerID=<?=$CustomerID?>" class="btn btn-secondary">Cancel</a>
</div>
</form>
</div>
</div>
<script src="../../bootstrap/bootstrap.bundle.min.js"></script>
</div>
</body>
</html>
customer_edit_db.php
<?php
/*
05.5050-PHP-MySQL-CRUD-Simple
customer_edit_db.php
Teeratus_R
Edit : 20230109
*/
session_start();
require_once "../../config/db-config.php";
$CustomerID = $_POST['CustomerID'];
$CustomerCodeRaw = trim($_POST['CustomerCode']);
$CustomerCode = strtoupper($CustomerCodeRaw);
$CompanyName = $_POST['CompanyName'];
$ContactName = $_POST['ContactName'];
$Address = $_POST['Address'];
$City = $_POST['City'];
// try {
// // Prepare statement
// $stmt = $conn->prepare($sql_customer);
// // execute the query
// $stmt->execute();
// echo $stmt->rowCount() . " records UPDATED successfully";
// } catch (PDOException $e) {
// echo $sql_costomer . "<br>" . $e->getMessage();
// }
try {
$sql_customer = "UPDATE Customers
SET
CustomerCode = '$CustomerCode',
CompanyName = '$CompanyName',
ContactName = '$ContactName',
Address = '$Address',
City = '$City'
WHERE CustomerID = '$CustomerID';";
$stmt = $conn->prepare($sql_customer);
$stmt->execute();
$conn = null;
$_SESSION['success'] = "Data has been updated succesfully";
header("location: ./customer_details.php?CustomerID={$CustomerID}");
} catch(PDOException $e) {
// echo $sql . "<br>" . $e->getMessage();
$_SESSION['error'] = "Data has not been updated succesfully". $e->getMessage();
header("location: ./customer_list.php");
}
// header("location: ./manage_customer.php?CustomerID={$CustomerID}");
?>
customer_delete.php
<?php
/*
customer_delete.php
Teeratus_R
*/
session_start();
require_once "../../config/db-config.php";
$CustomerID = $_GET['CustomerID'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>N-Delete</title>
<link rel="shortcut icon" href="../../webcomponents/favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="../../bootstrap/bootstrap.min.css">
</head>
<body>
<div class="container">
<?php
print_r($_POST);
echo "<hr>";
print_r($_GET);
echo "<hr>";
print_r($_SESSION);
echo "<hr>";
$sql_cust =
"SELECT
CustomerCode,
CompanyName,
ContactName,
Address,
City
FROM Customers
WHERE CustomerID = $CustomerID
;";
$stmt = $conn->prepare($sql_cust);
$stmt->execute();
$Customer_data = $stmt->fetch(PDO::FETCH_ASSOC);
// $CustomerID = $Customer_data['CustomerID'];
$CustomerCode = $Customer_data['CustomerCode'];
$CompanyName = $Customer_data['CompanyName'];
$ContactName = $Customer_data['ContactName'];
// $x = $Customer_data['x'];
?>
<h2 class="text-danger">Delete Customer ?</h2>
<div class="d-flex gap-3 justify-content-start">
<!-- <a href="customer_list.php" class="btn btn-primary">List</a> -->
</div>
<div class="row">
<div class="border col-sm-6 ">
<form action="customer_delete_db.php" method="post">
<div class="mb-3">
<label for="CustomerCode" class="form-label">CustomerCode</label>
<input type="text" class="form-control" name="CustomerCode" value="<?=$CustomerCode?>">
</div>
<div class="mb-3">
<label for="CompanyName" class="form-label">CompanyName</label>
<input type="text" class="form-control" name="CompanyName" value="<?=$CompanyName?>">
</div>
<div class="mb-3">
<label for="ContactName" class="form-label">ContactName</label>
<input type="text" class="form-control" name="ContactName" value="<?=$ContactName?>">
</div>
<div class="mb-3">
<label for="CustomerID" class="form-label">CustomerID</label>
<input type="text" class="form-control" name="CustomerID" value="<?=$CustomerID?>">
</div>
<div class="d-flex gap-2 justify-content-start mb-3">
<button type="submit" class="btn btn-danger">Confirm</button>
<a href="./customer_details.php?CustomerID=<?=$CustomerID?>" class="btn btn-secondary">Cancel</a>
</div>
</form>
</div>
</div>
</div>
<script src="../../bootstrap/bootstrap.bundle.min.js"></script>
</body>
</html>
customer_delete_db.php
<?php
// customer_delete_db.php
session_start();
require_once "../../config/db-config.php";
$CustomerID = $_POST['CustomerID'];
// $CustomerID = 97;
// $x = $_POST['x'];
print_r($CustomerID);
try {
// sql to delete a record
$sql_cust = "DELETE FROM Customers
WHERE CustomerID = $CustomerID
;";
// use exec() because no results are returned
$conn->exec($sql_cust);
$conn = null;
$_SESSION['success'] = "Data has been updated succesfully";
header("location: ./customer_list.php");
// echo "Record deleted successfully";
} catch(PDOException $e) {
// echo $sql_cust . "<br>" . $e->getMessage();
$_SESSION['error'] = "Data has not been updated succesfully";
header("location: ./customer_list.php");
// header("location: ./customer_list.php?id={$service_orders_id}");
}
?>
product_list.php
<?php
/*
05.5050-PHP-MySQL-CRUD-SimpleAll
product_list.php
Teeratus_R
Edit : 20230110
*/
session_start();
require_once "../../config/db-config.php";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="shortcut icon" href="../../webcomponents/favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="../../bootstrap/bootstrap.min.css">
<style>
thead {
background-color: #ffc;
}
</style>
</head>
<body>
<h4>Template Filename : product_list.php</h4>
<p>05.5050-PHP-MySQL-CRUD-SimpleAll</p>
<?php
print_r($_POST);
echo "<hr>";
print_r($_GET);
echo "<hr>";
print_r($_SESSION);
echo "<hr>";
?>
<?php
if (isset($_SESSION['error'])) {
?>
<div class="alert alert-danger" role="alert">
<?php
echo $_SESSION['error'];
unset($_SESSION['error']);
?>
</div>
<?php
}
?>
<?php
if (isset($_SESSION['success'])) {
?>
<div class="alert alert-success" role="alert">
<?php
echo $_SESSION['success'];
unset($_SESSION['success']);
?>
</div>
<?php
}
?>
<div class="d-flex gap-3 justify-content-start mb-3">
<a href="index.php" class="btn btn-primary">Home</a>
<a href="product_new.php" class="btn btn-primary">New</a>
<a href="product_list.php" class="btn btn-success">List</a>
<!-- <a href="product_edit.php" class="btn btn-warning disabled">Edit</a> -->
</div>
<table class="table table-striped table-bordered table-hover">
<thead>
<th>ProductName</th>
<th>SupplierID</th>
<th>SupplierName</th>
<th>CategoryID</th>
<th>CategoryName</th>
<th>QuantityPerUnit</th>
<th>UnitPrice</th>
<th>UnitInstock</th>
<th>Action</th>
<th>ProductID</th>
</thead>
<tbody>
<?php
$sql_product =
"SELECT
ProductID,
ProductName,
Products.SupplierID,
Suppliers.CompanyName,
Products.CategoryID,
Categories.CategoryName,
QuantityPerUnit,
UnitPrice,
UnitsInStock
FROM Products
LEFT JOIN Suppliers
ON Products.SupplierID = Suppliers.SupplierID
LEFT JOIN Categories
ON Products.CategoryID = Categories.CategoryID
WHERE Discontinued != 1
ORDER BY ProductID ASC
;";
$stmt = $conn->query($sql_product);
$stmt->execute();
$array_product = $stmt->fetchAll();
if (!$array_product) {
echo "<tr><td colspan='8' class='text-center'>No data found</td></tr>";
} else {
foreach ($array_product as $product_data) {
?>
<tr>
<td><?=$product_data['ProductName'];?></td>
<td><?=$product_data['SupplierID'];?></td>
<td><?=$product_data['CompanyName'];?></td>
<td><?=$product_data['CategoryID'];?></td>
<td><?=$product_data['CategoryName'];?></td>
<td><?=$product_data['QuantityPerUnit'];?></td>
<td><?=$product_data['UnitPrice'];?></td>
<td><?=$product_data['UnitsInStock'];?></td>
<td><a href="./product_details.php?ProductID=<?=$product_data['ProductID'];?>" class="btn btn-warning">Details</a></td>
<td><?=$product_data['ProductID'];?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
<script src="../../bootstrap/bootstrap.bundle.min.js"></script>
</body>
</html>
product_details.php
<?php
/*
05.5050-PHP-MySQL-CRUD-SimpleAll
product_details.php
Teeratus_R
Edit : 20230111
*/
session_start();
require_once "../../config/db-config.php";
$ProductID = $_GET['ProductID'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Details</title>
<link rel="shortcut icon" href="../../webcomponents/favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="../../bootstrap/bootstrap.min.css">
</head>
<body>
<div class="container">
<?php
print_r($_POST);
echo "<hr>";
print_r($_GET);
echo "<hr>";
print_r($_SESSION);
echo "<hr>";
?>
<h2>Details</h2>
<?php
if (isset($_SESSION['error'])) {
?>
<div class="alert alert-danger" role="alert">
<?php
echo $_SESSION['error'];
unset($_SESSION['error']);
?>
</div>
<?php
}
?>
<?php
if (isset($_SESSION['success'])) {
?>
<div class="alert alert-success" role="alert">
<?php
echo $_SESSION['success'];
unset($_SESSION['success']);
?>
</div>
<?php
}
?>
<?php
$sql_product =
"SELECT
ProductID,
ProductName,
Products.SupplierID,
Suppliers.CompanyName,
Products.CategoryID,
Categories.CategoryName,
QuantityPerUnit, UnitPrice, UnitsInStock,
UnitsOnOrder, ReorderLevel, Discontinued
FROM Products
LEFT JOIN Suppliers
ON Products.SupplierID = Suppliers.SupplierID
LEFT JOIN Categories
ON Products.CategoryID = Categories.CategoryID
WHERE ProductID = '$ProductID'
;";
$stmt = $conn->query($sql_product);
$stmt->execute();
$product = $stmt->fetch();
// $ProductID = $product['ProductID'];
$ProductName = $product['ProductName'];
$SupplierID = $product['SupplierID'];
$CompanyName = $product['CompanyName'];
$CategoryID = $product['CategoryID'];
$CategoryName = $product['CategoryName'];
$QuantityPerUnit = $product['QuantityPerUnit'];
$UnitPrice = $product['UnitPrice'];
$UnitsInStock = $product['UnitsInStock'];
$UnitsOnOrder = $product['UnitsOnOrder'];
$ReorderLevel = $product['ReorderLevel'];
$Discontinued = $product['Discontinued'];
?>
<div class="d-flex gap-2 justify-content-start">
<a href="index.php" class="btn btn-primary">Home</a>
<a href="product_new.php" class="btn btn-primary">New</a>
<a href="product_list.php" class="btn btn-success">List</a>
<a href="product_edit.php?ProductID=<?=$ProductID?>" class="btn btn-warning">Edit</a>
<a href="product_delete.php?ProductID=<?=$ProductID?>" class="btn btn-danger">Delete</a>
</div>
<form action="" method="post">
<!-- ProductName ProductID -->
<div class="row">
<div class="col-lg-10 col-sm-12">
<label for="ProductName" class="form-label">ProductName</label>
<input type="text" class="form-control" name="ProductName" value="<?=$ProductName;?>">
</div>
<div class="col-lg-2 col-sm-12">
<label for="ProductID" class="form-label">ProductID</label>
<input type="text" class="form-control" name="ProductID" value="<?=$ProductID;?>">
</div>
</div>
<!-- /ProductName ProductID -->
<!-- SupplierID CategoryID -->
<div class="row">
<div class="col-lg-2 col-sm-12">
<label for="SupplierID" class="form-label">SupplierID</label>
<input type="text" class="form-control" name="SupplierID" value="<?=$SupplierID;?>">
</div>
<div class="col-lg-4 col-sm-12">
<label for="CompanyName" class="form-label">CompanyName</label>
<input type="text" class="form-control" name="CompanyName" value="<?=$CompanyName;?>">
</div>
<div class="col-lg-2 col-sm-12">
<label for="CategoryID" class="form-label">CategoryID</label>
<input type="text" class="form-control" name="CategoryID" value="<?=$CategoryID;?>">
</div>
<div class="col-lg-4 col-sm-12">
<label for="CategoryName" class="form-label">CategoryName</label>
<input type="text" class="form-control" name="CategoryName" value="<?=$CategoryName;?>">
</div>
</div>
<!-- /SupplierID CategoryID -->
<!-- QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued -->
<div class="row">
<div class="col-lg-3 col-sm-12">
<label for="QuantityPerUnit" class="form-label">QuantityPerUnit</label>
<input type="text" class="form-control" name="QuantityPerUnit" value="<?=$QuantityPerUnit;?>">
</div>
<div class="col-lg-2 col-sm-12">
<label for="UnitPrice" class="form-label">UnitPrice</label>
<input type="text" class="form-control" name="UnitPrice" value="<?=$UnitPrice;?>">
</div>
<div class="col-lg-2 col-sm-12">
<label for="UnitsInStock" class="form-label">UnitsInStock</label>
<input type="text" class="form-control" name="UnitsInStock" value="<?=$UnitsInStock;?>">
</div>
<div class="col-lg-2 col-sm-12">
<label for="UnitsOnOrder" class="form-label">UnitsOnOrder</label>
<input type="text" class="form-control" name="UnitsOnOrder" value="<?=$UnitsOnOrder;?>">
</div>
<div class="col-lg-2 col-sm-12">
<label for="ReorderLevel" class="form-label">ReorderLevel</label>
<input type="text" class="form-control" name="ReorderLevel" value="<?=$ReorderLevel;?>">
</div>
<div class="col-lg-1 col-sm-12">
<label for="Discontinued" class="form-label">Discontinued</label>
<input type="text" class="form-control" name="Discontinued" value="<?=$Discontinued;?>">
</div>
</div>
<!-- /QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued -->
<div class="d-flex gap-2 justify-content-end">
<!-- <a href="product_edit_db.php?ProductID=<?=$ProductID?>" class="btn btn-warning">Update</a> -->
<!-- <a href="product_details.php?ProductID=<?=$ProductID?>" class="btn btn-warning">Cancel</a> -->
</div>
</form>
<script src="../../bootstrap/bootstrap.bundle.min.js"></script>
</div>
</body>
</html>
product_new.php
<?php
/*
05.5050-PHP-MySQL-CRUD-Simple
product_new.php
Teeratus_R
Edit : 20221229
*/
session_start();
require_once "../../config/db-config.php";
// $ProductID = $_GET['ProductID'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>N-Details</title>
<link rel="shortcut icon" href="../../webcomponents/favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="../../bootstrap/bootstrap.min.css">
</head>
<body>
<div class="container">
<h2>Template Filename : Create New Product</h2>
<div class="d-flex gap-2 justify-content-start">
<a href="index.php" class="btn btn-primary">Home</a>
<a href="product_new.php" class="btn btn-primary">New</a>
<a href="product_list.php" class="btn btn-success">List</a>
<!-- <a href="product_edit.php?ProductID=<?=$ProductID?>" class="btn btn-warning">Edit</a> -->
<!-- <a href="product_delete.php?ProductID=<?=$ProductID?>" class="btn btn-danger">Delete</a> -->
</div>
<div class="row">
<form action="product_new_db.php" method="post">
<!-- ProductName ProductID -->
<div class="row">
<div class="col-lg-10 col-sm-12">
<label for="ProductName" class="form-label">ProductName</label>
<input type="text" class="form-control" name="ProductName" required>
</div>
<!-- <div class="col-lg-2 col-sm-12">
<label for="ProductID" class="form-label">ProductID</label>
<input type="text" class="form-control" name="ProductID">
</div> -->
</div>
<!-- /ProductName ProductID -->
<!-- SupplierID CategoryID -->
<div class="row">
<div class="col-lg-6 col-sm-12">
<label for="SupplierID" class="form-label">SupplierID - CompanyName</label>
<!-- <input type="text" class="form-control" name="SupplierID" value="<?=$SupplierID;?>"> -->
<select name="SupplierID" class="form-select" required>
<option>
Select Supplier
</option>
<?php
// SQL for new select data
$sql_suppliers = "SELECT SupplierID, CompanyName
FROM Suppliers
ORDER BY CompanyName ASC
;";
$stmt = $conn->query($sql_suppliers);
$stmt->execute();
$SupplierQuery = $stmt->fetchAll();
foreach ($SupplierQuery as $SupplierResult) {
?>
<option value="<?php echo $SupplierResult['SupplierID'];?>">
<?php echo $SupplierResult['SupplierID'];?>
<?php echo $SupplierResult['CompanyName'];?>
</option>
<?php
}
?>
</select>
</div>
<div class="col-lg-6 col-sm-12">
<label for="CategoryID" class="form-label">CategoryID - CategoryName</label>
<select name="CategoryID" class="form-control form-select">
<option>
Select Category
</option>
<?php
// SQL for new select data
$sql_Category = "SELECT CategoryID, CategoryName
FROM Categories
ORDER BY CategoryName ASC
;";
$stmt = $conn->query($sql_Category);
$stmt->execute();
$CategoryQuery = $stmt->fetchAll();
foreach ($CategoryQuery as $CategoryResult) {
?>
<option value="<?php echo $CategoryResult['CategoryID'];?>">
<?php echo $CategoryResult['CategoryID'];?>
<?php echo $CategoryResult['CategoryName'];?>
</option>
<?php
}
?>
</select>
</div>
</div>
<!-- /SupplierID CategoryID -->
<!-- QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued -->
<div class="row">
<div class="col-lg-3 col-sm-12">
<label for="QuantityPerUnit" class="form-label">QuantityPerUnit</label>
<input type="text" class="form-control" name="QuantityPerUnit">
</div>
<div class="col-lg-2 col-sm-12">
<label for="UnitPrice" class="form-label">UnitPrice</label>
<input type="number" class="form-control" name="UnitPrice" required>
</div>
<div class="col-lg-2 col-sm-12">
<label for="UnitsInStock" class="form-label">UnitsInStock</label>
<input type="number" class="form-control" name="UnitsInStock" required placeholder="ถ้าไม่ทราบใส่ 0">
</div>
<div class="col-lg-2 col-sm-12">
<label for="UnitsOnOrder" class="form-label">UnitsOnOrder</label>
<input type="number" class="form-control" name="UnitsOnOrder" required placeholder="ถ้าไม่ทราบใส่ 0">
</div>
<div class="col-lg-2 col-sm-12">
<label for="ReorderLevel" class="form-label">ReorderLevel</label>
<input type="number" class="form-control" name="ReorderLevel" required placeholder="ถ้าไม่ทราบใส่ 0">
</div>
</div>
<div class="row">
<div class="col-lg-3 col-sm-12">
<label for="Discontinued" class="form-label">Discontinued</label>
<select name="Discontinued" class="form-control form-select">
<option value="0">Active</option>
<option value="1">Discontinued</option>
</select>
</div>
</div>
<!-- /QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued -->
<div class="d-flex gap-2 justify-content-end m-3">
<button type="submit" class="btn btn-success">Update</button>
<a href="product_details.php?ProductID=<?=$ProductID?>" class="btn btn-secondary">Cancel</a>
</div>
</form>
<!-- /class="row" -->
</div>
</div>
<script src="../../bootstrap/bootstrap.bundle.min.js"></script>
</body>
</html>
product_new_db.php
<?php
/*
05.5050-PHP-MySQL-CRUD-Simple
product_new_db.php
Teeratus_R
Edit : 20230111
*/
session_start();
require_once "../../config/db-config.php";
$ProductID = $_POST['ProductID'];
$ProductName = $_POST['ProductName'];
$SupplierID = $_POST['SupplierID'];
$CategoryID = $_POST['CategoryID'];
$QuantityPerUnit = $_POST['QuantityPerUnit'];
$UnitPrice = $_POST['UnitPrice'];
$UnitsInStock = $_POST['UnitsInStock'];
$UnitsOnOrder = $_POST['UnitsOnOrder'];
$ReorderLevel = $_POST['ReorderLevel'];
$Discontinued = $_POST['Discontinued'];
try {
$sql_product = "INSERT INTO Products
(
ProductName,
SupplierID,
CategoryID,
QuantityPerUnit,
UnitPrice,
UnitsInStock,
UnitsOnOrder,
ReorderLevel,
Discontinued
)
VALUE
(
'$ProductName',
'$SupplierID',
'$CategoryID',
'$QuantityPerUnit',
'$UnitPrice',
'$UnitsInStock',
'$UnitsOnOrder',
'$ReorderLevel',
'$Discontinued'
)
;";
$conn->exec($sql_product);
$last_id = $conn->lastInsertId();
$conn = null;
$_SESSION['success'] = "Data has been updated succesfully";
header("location: ./product_details.php?ProductID={$last_id}");
} catch(PDOException $e) {
// echo $sql . "<br>" . $e->getMessage();
$_SESSION['error'] = "Data has not been updated succesfully". $e->getMessage();
header("location: ./product_list.php");
}
// header("location: ./manage_product.php?ProductID={$ProductID}");
?>
product_edit.php
<?php
/*
05.5050-PHP-MySQL-CRUD-SimpleAll
product_edit.php
Teeratus_R
Edit : 20230111
*/
session_start();
require_once "../../config/db-config.php";
$ProductID = $_GET['ProductID'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Edit</title>
<link rel="shortcut icon" href="../../webcomponents/favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="../../bootstrap/bootstrap.min.css">
</head>
<body>
<div class="container">
<?php
print_r($_POST);
echo "<hr>";
print_r($_GET);
echo "<hr>";
print_r($_SESSION);
echo "<hr>";
?>
<h2>Edit</h2>
<?php
if (isset($_SESSION['error'])) {
?>
<div class="alert alert-danger" role="alert">
<?php
echo $_SESSION['error'];
unset($_SESSION['error']);
?>
</div>
<?php
}
?>
<?php
if (isset($_SESSION['success'])) {
?>
<div class="alert alert-success" role="alert">
<?php
echo $_SESSION['success'];
unset($_SESSION['success']);
?>
</div>
<?php
}
?>
<?php
$sql_product =
"SELECT
ProductID,
ProductName,
Products.SupplierID,
Suppliers.CompanyName,
Products.CategoryID,
Categories.CategoryName,
QuantityPerUnit, UnitPrice, UnitsInStock,
UnitsOnOrder, ReorderLevel, Discontinued
FROM Products
LEFT JOIN Suppliers
ON Products.SupplierID = Suppliers.SupplierID
LEFT JOIN Categories
ON Products.CategoryID = Categories.CategoryID
WHERE ProductID = '$ProductID'
;";
$stmt = $conn->query($sql_product);
$stmt->execute();
$product = $stmt->fetch();
// $ProductID = $product['ProductID'];
$ProductName = $product['ProductName'];
$SupplierID = $product['SupplierID'];
$CompanyName = $product['CompanyName'];
$CategoryID = $product['CategoryID'];
$CategoryName = $product['CategoryName'];
$QuantityPerUnit = $product['QuantityPerUnit'];
$UnitPrice = $product['UnitPrice'];
$UnitsInStock = $product['UnitsInStock'];
$UnitsOnOrder = $product['UnitsOnOrder'];
$ReorderLevel = $product['ReorderLevel'];
$Discontinued = $product['Discontinued'];
?>
<div class="d-flex gap-2 justify-content-start">
<a href="index.php" class="btn btn-primary">Home</a>
<a href="product_new.php" class="btn btn-primary">New</a>
<a href="product_list.php" class="btn btn-success">List</a>
<a href="product_edit.php?ProductID=<?=$ProductID?>" class="btn btn-warning">Edit</a>
<a href="product_delete.php?ProductID=<?=$ProductID?>" class="btn btn-danger">Delete</a>
</div>
<form action="product_edit_db.php" method="post">
<!-- ProductName ProductID -->
<div class="row">
<div class="col-lg-10 col-sm-12">
<label for="ProductName" class="form-label">ProductName</label>
<input type="text" class="form-control" name="ProductName" value="<?=$ProductName;?>">
</div>
<div class="col-lg-2 col-sm-12">
<label for="ProductID" class="form-label">ProductID</label>
<input type="text" class="form-control" name="ProductID" value="<?=$ProductID;?>" readonly>
</div>
</div>
<!-- /ProductName ProductID -->
<!-- SupplierID CategoryID -->
<div class="row">
<div class="col-lg-6 col-sm-12">
<label for="SupplierID" class="form-label">SupplierID - CompanyName</label>
<!-- <input type="text" class="form-control" name="SupplierID" value="<?=$SupplierID;?>"> -->
<select name="SupplierID" class="form-control form-select">
<?php
// SQL selected data
if (isset($product['SupplierID'])) {
// $SupplierID = $product['SupplierID'];
// $CompanyName = $product['CompanyName'];
} else {
$CompanyName = "N/A";
}
?>
<option value="<?=$SupplierID;?>">
<?php echo $SupplierID." ".$CompanyName;?>
</option>
<?php
// SQL for new select data
$sql_suppliers = "SELECT SupplierID, CompanyName
FROM Suppliers
ORDER BY CompanyName ASC
;";
$stmt = $conn->query($sql_suppliers);
$stmt->execute();
$SupplierQuery = $stmt->fetchAll();
foreach ($SupplierQuery as $SupplierResult) {
?>
<option value="<?php echo $SupplierResult['SupplierID'];?>">
<?php echo $SupplierResult['SupplierID'];?>
<?php echo $SupplierResult['CompanyName'];?>
</option>
<?php
}
?>
</select>
</div>
<div class="col-lg-6 col-sm-12">
<label for="CategoryID" class="form-label">CategoryID - CategoryName</label>
<select name="CategoryID" class="form-control form-select">
<?php
// SQL selected data
if (isset($product['CategoryID'])) {
// $CategoryID = $product['CategoryID'];
// $CategoryName = $product['CategoryName'];
} else {
$CategoryName = "N/A";
}
?>
<option value="<?=$CategoryID;?>">
<?php echo $CategoryID." ".$CategoryName;?>
</option>
<?php
// SQL for new select data
$sql_Category = "SELECT CategoryID, CategoryName
FROM Categories
ORDER BY CategoryName ASC
;";
$stmt = $conn->query($sql_Category);
$stmt->execute();
$CategoryQuery = $stmt->fetchAll();
foreach ($CategoryQuery as $CategoryResult) {
?>
<option value="<?php echo $CategoryResult['CategoryID'];?>">
<?php echo $CategoryResult['CategoryID'];?>
<?php echo $CategoryResult['CategoryName'];?>
</option>
<?php
}
?>
</select>
</div>
</div>
<!-- /SupplierID CategoryID -->
<!-- QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued -->
<div class="row">
<div class="col-lg-3 col-sm-12">
<label for="QuantityPerUnit" class="form-label">QuantityPerUnit</label>
<input type="text" class="form-control" name="QuantityPerUnit" value="<?=$QuantityPerUnit;?>">
</div>
<div class="col-lg-2 col-sm-12">
<label for="UnitPrice" class="form-label">UnitPrice</label>
<input type="text" class="form-control" name="UnitPrice" value="<?=$UnitPrice;?>">
</div>
<div class="col-lg-2 col-sm-12">
<label for="UnitsInStock" class="form-label">UnitsInStock</label>
<input type="text" class="form-control" name="UnitsInStock" value="<?=$UnitsInStock;?>">
</div>
<div class="col-lg-2 col-sm-12">
<label for="UnitsOnOrder" class="form-label">UnitsOnOrder</label>
<input type="text" class="form-control" name="UnitsOnOrder" value="<?=$UnitsOnOrder;?>">
</div>
<div class="col-lg-2 col-sm-12">
<label for="ReorderLevel" class="form-label">ReorderLevel</label>
<input type="text" class="form-control" name="ReorderLevel" value="<?=$ReorderLevel;?>">
</div>
<div class="col-lg-1 col-sm-12">
<label for="Discontinued" class="form-label">Discontinued</label>
<input type="text" class="form-control" name="Discontinued" value="<?=$Discontinued;?>">
</div>
</div>
<!-- /QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued -->
<div class="d-flex gap-2 justify-content-end m-3">
<button type="submit" class="btn btn-success">Update</button>
<a href="product_details.php?ProductID=<?=$ProductID?>" class="btn btn-secondary">Cancel</a>
</div>
</form>
<script src="../../bootstrap/bootstrap.bundle.min.js"></script>
</div>
</body>
</html>
product_edit_db.php
<?php
/*
05.5050-PHP-MySQL-CRUD-Simple
product_edit_db.php
Teeratus_R
Edit : 20230111
*/
session_start();
require_once "../../config/db-config.php";
$ProductID = $_POST['ProductID'];
$ProductName = $_POST['ProductName'];
$SupplierID = $_POST['SupplierID'];
$CategoryID = $_POST['CategoryID'];
$QuantityPerUnit = $_POST['QuantityPerUnit'];
$UnitPrice = $_POST['UnitPrice'];
$UnitsInStock = $_POST['UnitsInStock'];
$UnitsOnOrder = $_POST['UnitsOnOrder'];
$ReorderLevel = $_POST['ReorderLevel'];
$Discontinued = $_POST['Discontinued'];
try {
$sql_product = "INSERT INTO Products
(
ProductName,
SupplierID,
CategoryID,
QuantityPerUnit,
UnitPrice,
UnitsInStock,
UnitsOnOrder,
ReorderLevel,
Discontinued
)
VALUE
(
'$ProductName',
'$SupplierID',
'$CategoryID',
'$QuantityPerUnit',
'$UnitPrice',
'$UnitsInStock',
'$UnitsOnOrder',
'$ReorderLevel',
'$Discontinued'
)
;";
$conn->exec($sql_product);
$last_id = $conn->lastInsertId();
$conn = null;
$_SESSION['success'] = "Data has been updated succesfully";
header("location: ./product_details.php?ProductID={$last_id}");
} catch(PDOException $e) {
// echo $sql . "<br>" . $e->getMessage();
$_SESSION['error'] = "Data has not been updated succesfully". $e->getMessage();
header("location: ./product_list.php");
}
// header("location: ./manage_product.php?ProductID={$ProductID}");
?>
product_delete.php
<?php
/*
05.5050-PHP-MySQL-CRUD-SimpleAll
product_delete.php
Teeratus_R
Edit : 20230111
*/
session_start();
require_once "../../config/db-config.php";
$ProductID = $_GET['ProductID'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Delete</title>
<link rel="shortcut icon" href="../../webcomponents/favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="../../bootstrap/bootstrap.min.css">
</head>
<body>
<div class="container">
<?php
print_r($_POST);
echo "<hr>";
print_r($_GET);
echo "<hr>";
print_r($_SESSION);
echo "<hr>";
?>
<h2 class="bg-danger p-3 text-white">Delete [Pseudo]</h2>
<?php
$sql_product =
"SELECT
ProductID,
ProductName,
Products.SupplierID,
Suppliers.CompanyName,
Products.CategoryID,
Categories.CategoryName,
QuantityPerUnit, UnitPrice, UnitsInStock,
UnitsOnOrder, ReorderLevel, Discontinued
FROM Products
LEFT JOIN Suppliers
ON Products.SupplierID = Suppliers.SupplierID
LEFT JOIN Categories
ON Products.CategoryID = Categories.CategoryID
WHERE ProductID = '$ProductID'
;";
$stmt = $conn->query($sql_product);
$stmt->execute();
$product = $stmt->fetch();
// $ProductID = $product['ProductID'];
$ProductName = $product['ProductName'];
$SupplierID = $product['SupplierID'];
$CompanyName = $product['CompanyName'];
$CategoryID = $product['CategoryID'];
$CategoryName = $product['CategoryName'];
$QuantityPerUnit = $product['QuantityPerUnit'];
$UnitPrice = $product['UnitPrice'];
$UnitsInStock = $product['UnitsInStock'];
$UnitsOnOrder = $product['UnitsOnOrder'];
$ReorderLevel = $product['ReorderLevel'];
$Discontinued = $product['Discontinued'];
?>
<div class="d-flex gap-2 justify-content-start">
<a href="index.php" class="btn btn-primary">Home</a>
<a href="product_new.php" class="btn btn-primary">New</a>
<a href="product_list.php" class="btn btn-success">List</a>
<a href="product_edit.php?ProductID=<?=$ProductID?>" class="btn btn-warning">Edit</a>
<a href="product_delete.php?ProductID=<?=$ProductID?>" class="btn btn-danger">Delete</a>
</div>
<form action="product_delete_db.php" method="post">
<!-- ProductName ProductID -->
<div class="row">
<div class="col-lg-10 col-sm-12">
<label for="ProductName" class="form-label">ProductName</label>
<input type="text" class="form-control" name="ProductName" value="<?=$ProductName;?>">
</div>
<div class="col-lg-2 col-sm-12">
<label for="ProductID" class="form-label">ProductID</label>
<input type="text" class="form-control" name="ProductID" value="<?=$ProductID;?>">
</div>
</div>
<!-- /ProductName ProductID -->
<!-- SupplierID CategoryID -->
<div class="row">
<div class="col-lg-2 col-sm-12">
<label for="SupplierID" class="form-label">SupplierID</label>
<input type="text" class="form-control" name="SupplierID" value="<?=$SupplierID;?>">
</div>
<div class="col-lg-4 col-sm-12">
<label for="CompanyName" class="form-label">CompanyName</label>
<input type="text" class="form-control" name="CompanyName" value="<?=$CompanyName;?>">
</div>
<div class="col-lg-2 col-sm-12">
<label for="CategoryID" class="form-label">CategoryID</label>
<input type="text" class="form-control" name="CategoryID" value="<?=$CategoryID;?>">
</div>
<div class="col-lg-4 col-sm-12">
<label for="CategoryName" class="form-label">CategoryName</label>
<input type="text" class="form-control" name="CategoryName" value="<?=$CategoryName;?>">
</div>
</div>
<!-- /SupplierID CategoryID -->
<!-- QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued -->
<div class="row">
<div class="col-lg-3 col-sm-12">
<label for="QuantityPerUnit" class="form-label">QuantityPerUnit</label>
<input type="text" class="form-control" name="QuantityPerUnit" value="<?=$QuantityPerUnit;?>">
</div>
<div class="col-lg-2 col-sm-12">
<label for="UnitPrice" class="form-label">UnitPrice</label>
<input type="text" class="form-control" name="UnitPrice" value="<?=$UnitPrice;?>">
</div>
<div class="col-lg-2 col-sm-12">
<label for="UnitsInStock" class="form-label">UnitsInStock</label>
<input type="text" class="form-control" name="UnitsInStock" value="<?=$UnitsInStock;?>">
</div>
<div class="col-lg-2 col-sm-12">
<label for="UnitsOnOrder" class="form-label">UnitsOnOrder</label>
<input type="text" class="form-control" name="UnitsOnOrder" value="<?=$UnitsOnOrder;?>">
</div>
<div class="col-lg-2 col-sm-12">
<label for="ReorderLevel" class="form-label">ReorderLevel</label>
<input type="text" class="form-control" name="ReorderLevel" value="<?=$ReorderLevel;?>">
</div>
<div class="col-lg-1 col-sm-12">
<label for="Discontinued" class="form-label">Discontinued</label>
<input type="text" class="form-control" name="Discontinued" value="<?=$Discontinued;?>">
</div>
</div>
<!-- /QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued -->
<div class="d-flex gap-2 justify-content-end m-3">
<button type="submit" class="btn btn-danger">Delete</button>
<a href="product_details.php?ProductID=<?=$ProductID?>" class="btn btn-warning">Cancel</a>
</div>
</form>
<script src="../../bootstrap/bootstrap.bundle.min.js"></script>
</div>
</body>
</html>
product_delete_db.php
<?php
/*
05.5050-PHP-MySQL-CRUD-SimpleAll
product_delete_db.php [Pseudo]
Teeratus_R
Edit : 20230113
*/
session_start();
require_once "../../config/db-config.php";
$ProductID = $_POST['ProductID'];
// $ProductName = $_POST['ProductName'];
// $SupplierID = $_POST['SupplierID'];
// $CategoryID = $_POST['CategoryID'];
// $QuantityPerUnit = $_POST['QuantityPerUnit'];
// $UnitPrice = $_POST['UnitPrice'];
// $UnitsInStock = $_POST['UnitsInStock'];
// $UnitsOnOrder = $_POST['UnitsOnOrder'];
// $ReorderLevel = $_POST['ReorderLevel'];
// $Discontinued = $_POST['Discontinued'];
$Discontinued = 1;
try {
$sql_product = "UPDATE Products
SET
Discontinued = '$Discontinued'
WHERE ProductID = '$ProductID'
;";
$stmt = $conn->prepare($sql_product);
$stmt->execute();
$conn = null;
$_SESSION['success'] = "Data has been updated succesfully";
header("location: ./product_list.php");
} catch(PDOException $e) {
// echo $sql . "<br>" . $e->getMessage();
$_SESSION['error'] = "Data has not been updated succesfully". $e->getMessage();
header("location: ./product_list.php");
}
// header("location: ./manage_product.php?ProductID={$ProductID}");
?>
category_list.php
<?php
/*
05.5050-PHP-MySQL-CRUD-SimpleAll
category_list.php
Teeratus_R
Edit : 20230110
*/
session_start();
require_once "../../config/db-config.php";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="shortcut icon" href="../../webcomponents/favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="../../bootstrap/bootstrap.min.css">
<style>
thead {
background-color: #ffc;
}
</style>
</head>
<body>
<h4>Template Filename : category_list.php</h4>
<p>05.5050-PHP-MySQL-CRUD-SimpleAll</p>
<?php
print_r($_POST);
echo "<hr>";
print_r($_GET);
echo "<hr>";
print_r($_SESSION);
echo "<hr>";
?>
<?php
if (isset($_SESSION['error'])) {
?>
<div class="alert alert-danger" role="alert">
<?php
echo $_SESSION['error'];
unset($_SESSION['error']);
?>
</div>
<?php
}
?>
<?php
if (isset($_SESSION['success'])) {
?>
<div class="alert alert-success" role="alert">
<?php
echo $_SESSION['success'];
unset($_SESSION['success']);
?>
</div>
<?php
}
?>
<div class="d-flex gap-3 justify-content-start mb-3">
<a href="index.php" class="btn btn-primary">Home</a>
<a href="category_new.php" class="btn btn-primary">New</a>
<a href="category_list.php" class="btn btn-success">List</a>
<!-- <a href="category_edit.php" class="btn btn-warning disabled">Edit</a> -->
</div>
<table class="table table-striped table-bordered table-hover">
<thead>
<th>CategoryName</th>
<th>Description</th>
<th>Action</th>
<th>CategoryID</th>
</thead>
<tbody>
<?php
$sql_category =
"SELECT
CategoryID,
CategoryName,
Description
FROM Categories
ORDER BY CategoryName ASC
;";
$stmt = $conn->query($sql_category);
$stmt->execute();
$array_category = $stmt->fetchAll();
if (!$array_category) {
echo "<tr><td colspan='8' class='text-center'>No data found</td></tr>";
} else {
foreach ($array_category as $category_data) {
?>
<tr>
<td><?=$category_data['CategoryName'];?></td>
<td><?=$category_data['Description'];?></td>
<td><a href="./category_details.php?CategoryID=<?=$category_data['CategoryID'];?>" class="btn btn-warning">Details</a></td>
<td><?=$category_data['CategoryID'];?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
<script src="../../bootstrap/bootstrap.bundle.min.js"></script>
</body>
</html>
category_details.php
<?php
/*
05.5050-PHP-MySQL-CRUD-SimpleAll
category_details.php
Teeratus_R
Edit : 20230111
*/
session_start();
require_once "../../config/db-config.php";
$CategoryID = $_GET['CategoryID'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Details</title>
<link rel="shortcut icon" href="../../webcomponents/favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="../../bootstrap/bootstrap.min.css">
</head>
<body>
<div class="container">
<?php
print_r($_POST);
echo "<hr>";
print_r($_GET);
echo "<hr>";
print_r($_SESSION);
echo "<hr>";
?>
<h2>Details</h2>
<?php
if (isset($_SESSION['error'])) {
?>
<div class="alert alert-danger" role="alert">
<?php
echo $_SESSION['error'];
unset($_SESSION['error']);
?>
</div>
<?php
}
?>
<?php
if (isset($_SESSION['success'])) {
?>
<div class="alert alert-success" role="alert">
<?php
echo $_SESSION['success'];
unset($_SESSION['success']);
?>
</div>
<?php
}
?>
<?php
$sql_category =
"SELECT
CategoryID,
CategoryName,
Description
FROM Categories
WHERE CategoryID = '$CategoryID'
;";
$stmt = $conn->query($sql_category);
$stmt->execute();
$category = $stmt->fetch();
// $CategoryID = $category['CategoryID'];
$CategoryName = $category['CategoryName'];
$Description = $category['Description'];
?>
<div class="d-flex gap-3 justify-content-start">
<a href="category_new.php" class="btn btn-primary">New</a>
<a href="category_list.php" class="btn btn-success">List</a>
</div>
<div class="row">
<div class="col-lg-12">
<table class="table table-striped">
<thead>
<tr>
<th>CategoryName</th>
<th>Description</th>
<th>Acction</th>
<th>CategoryID</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<?=$CategoryName?>
</td>
<td>
<?=$Description?>
</td>
<td>
<a href="category_edit.php?CategoryID=<?=$CategoryID?>" class="btn btn-warning">Edit</a>
<a href="category_delete.php?CategoryID=<?=$CategoryID?>" class="btn btn-danger">Delete</a>
</td>
<td><?=$CategoryID?></td>
</tr>
</tbody>
</table>
</div>
</div>
<script src="../../bootstrap/bootstrap.bundle.min.js"></script>
</div>
</body>
</html>
category_new.php
<?php
/*
05.5050-PHP-MySQL-CRUD-SimpleAll
category_new.php
Teeratus_R
Edit : 20221229
*/
session_start();
require_once "../../config/db-config.php";
// $CategoryID = $_GET['CategoryID'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>N-Details</title>
<link rel="shortcut icon" href="../../webcomponents/favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="../../bootstrap/bootstrap.min.css">
</head>
<body>
<div class="container">
<h2>Template Filename : Create New Category</h2>
<?php
print_r($_POST);
echo "<hr>";
print_r($_GET);
echo "<hr>";
print_r($_SESSION);
echo "<hr>";
?>
<div class="d-flex gap-3 justify-content-start">
<!-- <a href="category_list.php" class="btn btn-primary">List</a> -->
</div>
<div class="row">
<div class="border col-sm-6 ">
<form action="category_new_db.php" method="post">
<div class="mb-3">
<label for="CategoryName" class="form-label">CategoryName</label>
<input type="text" class="form-control" name="CategoryName" required>
</div>
<div class="mb-3">
<label for="Description" class="form-label">Description</label>
<input type="text" class="form-control" name="Description" required>
</div>
<div class="d-flex gap-2 justify-content-end mb-3">
<button type="submit" class="btn btn-primary">Submit</button>
<a href="./category_list.php" class="btn btn-secondary">Cancel</a>
</div>
</form>
</div>
<!-- /class="row" -->
</div>
</div>
<script src="../../bootstrap/bootstrap.bundle.min.js"></script>
</body>
</html>
category_new_db.php
<?php
/*
05.5050-PHP-MySQL-CRUD-SimpleAll
category_new_db.php
Teeratus_R
Edit : 20221229
*/
session_start();
require_once '../../config/db-config.php';
$CategoryName = trim($_POST['CategoryName']);
$Description = $_POST['Description'];
// $x = $_POST['x'];
try {
$sql_category = "INSERT INTO Categories
(CategoryName, Description)
VALUES
(
'$CategoryName',
'$Description'
)";
$conn->exec($sql_category);
$last_id = $conn->lastInsertId();
$conn = null;
$_SESSION['success'] = "Data has been updated succesfully";
// header("location: ./category_list.php");
header("location: ./category_details.php?CategoryID={$last_id}");
} catch(PDOException $e) {
// echo $sql . "<br>" . $e->getMessage();
$_SESSION['error'] = "Data has not been updated succesfully" . $e->getMessage();
header("location: ./category_list.php");
}
?>
category_edit.php
<?php
/*
05.5050-PHP-MySQL-CRUD-Simple
category_edit.php
Teeratus_R
Edit : 20230109
*/
session_start();
require_once "../../config/db-config.php";
$CategoryID = $_GET['CategoryID'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>N-Details</title>
<link rel="shortcut icon" href="../../webcomponents/favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="../../bootstrap/bootstrap.min.css">
</head>
<body>
<div class="container">
<?php
print_r($_POST);
echo "<hr>";
print_r($_GET);
echo "<hr>";
print_r($_SESSION);
echo "<hr>";
?>
<h2>Edit Category</h2>
<?php
$sql_category =
"SELECT
CategoryID,
CategoryName,
Description
FROM Categories
WHERE CategoryID = $CategoryID
;";
$stmt = $conn->query($sql_category);
$stmt->execute();
$category = $stmt->fetch();
$CategoryName = $category['CategoryName'];
$Description = $category['Description'];
?>
<div class="d-flex gap-3 justify-content-start mb-3">
<a href="category_list.php" class="btn btn-primary">List</a>
</div>
<div class="row">
<div class="border col-sm-6">
<form action="category_edit_db.php" method="post">
<input type="hidden" name="CategoryID" value="<?=$CategoryID?>">
<div class="mb-3">
<label for="CategoryName" class="form-label">CategoryName</label>
<input type="text" class="form-control" name="CategoryName" value="<?=$CategoryName?>" required>
</div>
<div class="mb-3">
<label for="Description" class="form-label">Description</label>
<input type="text" class="form-control" name="Description" value="<?=$Description?>" required>
</div>
<div class="d-flex gap-2 justify-content-end mb-3">
<button type="submit" class="btn btn-primary">Submit</button>
<a href="./category_details.php?CategoryID=<?=$CategoryID;?>" class="btn btn-secondary">Cancel</a>
</div>
</form>
</div>
</div>
<script src="../../bootstrap/bootstrap.bundle.min.js"></script>
</div>
</body>
</html>
category_edit_db.php
<?php
/*
05.5050-PHP-MySQL-CRUD-SimpleAll
category_edit_db.php
Teeratus_R
Edit : 20230111
*/
session_start();
require_once "../../config/db-config.php";
$CategoryID = $_POST['CategoryID'];
$CategoryName = trim($_POST['CategoryName']);
$Description = $_POST['Description'];
try {
$sql_category = "UPDATE Categories
SET
CategoryName = '$CategoryName',
Description = '$Description'
WHERE CategoryID = '$CategoryID';";
$stmt = $conn->prepare($sql_category);
$stmt->execute();
$conn = null;
$_SESSION['success'] = "Data has been updated succesfully";
header("location: ./category_details.php?CategoryID={$CategoryID}");
} catch(PDOException $e) {
// echo $sql . "<br>" . $e->getMessage();
$_SESSION['error'] = "Data has not been updated succesfully". $e->getMessage();
header("location: ./category_list.php");
}
// header("location: ./manage_category.php?CategoryID={$CategoryID}");
?>
category_delete.php
<?php
/*
category_delete.php
Teeratus_R
*/
session_start();
require_once "../../config/db-config.php";
$CategoryID = $_GET['CategoryID'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>N-Delete</title>
<link rel="shortcut icon" href="../../webcomponents/favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="../../bootstrap/bootstrap.min.css">
</head>
<body>
<div class="container">
<?php
print_r($_POST);
echo "<hr>";
print_r($_GET);
echo "<hr>";
print_r($_SESSION);
echo "<hr>";
$sql_category =
"SELECT
CategoryName,
Description
FROM Categories
WHERE CategoryID = $CategoryID
;";
$stmt = $conn->prepare($sql_category);
$stmt->execute();
$Category_data = $stmt->fetch(PDO::FETCH_ASSOC);
// $CategoryID = $Category_data['CategoryID'];
$CategoryName = $Category_data['CategoryName'];
$Description = $Category_data['Description'];
// $x = $Category_data['x'];
?>
<h2 class="text-danger">Delete Category ?</h2>
<div class="d-flex gap-3 justify-content-start">
<!-- <a href="category_list.php" class="btn btn-primary">List</a> -->
</div>
<div class="row">
<div class="border col-sm-6 ">
<form action="category_delete_db.php" method="post">
<div class="mb-3">
<label for="CategoryName" class="form-label">CategoryName</label>
<input type="text" class="form-control" name="CategoryName" value="<?=$CategoryName?>">
</div>
<div class="mb-3">
<label for="Description" class="form-label">Description</label>
<input type="text" class="form-control" name="Description" value="<?=$Description?>">
</div>
<div class="mb-3">
<label for="CategoryID" class="form-label">CategoryID</label>
<input type="text" class="form-control" name="CategoryID" value="<?=$CategoryID?>">
</div>
<div class="d-flex gap-2 justify-content-start mb-3">
<button type="submit" class="btn btn-danger">Confirm</button>
<a href="./category_details.php?CategoryID=<?=$CategoryID?>" class="btn btn-secondary">Cancel</a>
</div>
</form>
</div>
</div>
</div>
<script src="../../bootstrap/bootstrap.bundle.min.js"></script>
</body>
</html>
category_delete_db.php
<?php
session_start();
require_once "../../config/db-config.php";
$CategoryID = $_POST['CategoryID'];
// $CategoryID = 97;
// $x = $_POST['x'];
// print_r($CategoryID);
try {
// sql to delete a record
$sql_category = "DELETE FROM Categories
WHERE CategoryID = $CategoryID
;";
// use exec() because no results are returned
$conn->exec($sql_category);
$conn = null;
$_SESSION['success'] = "Data has been updated succesfully";
header("location: ./category_list.php");
// echo "Record deleted successfully";
} catch(PDOException $e) {
// echo $sql_category . "<br>" . $e->getMessage();
$_SESSION['error'] = "Data has not been updated succesfully";
header("location: ./category_list.php");
// header("location: ./category_list.php?id={$service_orders_id}");
}
?>
Document in project
You can Download PDF file.