Entire code posted below:
[php]
<?php
// DATABASE CODE
class Database {
private static $dbName = "crud_tutorial";
private static $dbHost = "localhost";
private static $dbUsername = "root";
private static $dbPassword = "";
private static $connection = null;
public function __construct() {
die("Init function is not allowed");
}
public static function connect() {
// One connection through whole application
if (null == self::$connection) {
try {
self::$connection = new PDO("mysql:host=".self::$dbHost.";"."dbname=".self::$dbName,
self::$dbUsername, self::$dbPassword);
} catch(PDOException $e) {
die($e->getMessage());
}
}
return self::$connection;
}
public static function disconnect() {
self::$connection = null;
}
}
?>
<?php // INDEX CODE ?>
PHP CRUD Grid
Create
Name |
Email Address |
Mobile Number |
Action |
<?php
include("database.php");
$pdo = Database::connect();
$sql = "SELECT * FROM customers ORDER BY id DESC";
foreach ($pdo->query($sql) as $row) {
echo "";
echo "" . $row["name"] . " | ";
echo "" . $row["email"] . " | ";
echo "" . $row["mobile"] . " | ";
echo "";
echo 'Read';
echo " ";
echo 'Update';
echo " ";
echo 'Delete';
echo " | ";
echo "
";
}
Database::disconnect();
?>
<?php
require("database.php");
if (!empty($_POST)) {
// Keep track of validation errors
$nameError = null;
$emailError = null;
$mobileError = null;
// Keep track post values
$name = $_POST["name"];
$email = $_POST["email"];
$mobile = $_POST["mobile"];
// Validate input
$valid = true;
if (empty($name)) {
$nameError = "Please enter Name";
$valid = false;
}
if (empty($email)) {
$emailError = "Please enter Email Address";
$valid = false;
}
if (empty($mobile)) {
$mobileError = "Please enter Mobile Number";
$valid = false;
}
// Insert data
if ($valid) {
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = "INSERT INTO customers (name, email, mobile) values (?, ?, ?)";
$stmt = $pdo->prepare($query);
$stmt->bindValue(1, $name, PDO::PARAM_STR);
$stmt->bindValue(2, $email, PDO::PARAM_STR);
$stmt->bindValue(3, $mobile, PDO::PARAM_INT);
$stmt->execute();
Database::disconnect();
header("Location: index.php");
}
}
?>
<?php // CREATE CODE ?>
Create a Customer
<form class="form=horizontal" action="create.php" method="post">
<div class="control-group <?php echo !empty($nameError) ? "error" : ""; ?>">
<label class="control-label">Name</label>
<div class="controls">
<input type="text" name="name" placeholder="Name" value="<?php
echo !empty($name) ? $name : ""; ?>" />
<?php if (!empty($nameError)): ?>
<span class="help-inline"><?php echo $nameError; ?></span>
<?php endif; ?>
</div>
</div>
<div class="control-group <?php echo !empty($emailError) ? "error" : ""; ?>">
<label class="control-label">Email Address</label>
<div class="controls">
<input type="text" name="email" placeholder="Email Address" value="<?php
echo !empty($email) ? $email : ""; ?>" />
<?php if (!empty($emailError)): ?>
<span class="help-inline"><?php echo $emailError; ?></span>
<?php endif; ?>
</div>
</div>
<div class="control-group <?php echo !empty($mobileError) ? "error" : ""; ?>">
<label class="control-label">Mobile Number</label>
<div class="controls">
<input type="text" name="mobile" placeholder="Mobile Number" value="<?php
echo !empty($mobile) ? $mobile : ""; ?>" />
<?php if (!empty($mobileError)): ?>
<span class="help-inline"><?php echo $mobileError; ?></span>
<?php endif; ?>
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-success">Create</button>
<a class="btn" href="index.php">Back</a>
</div>
</form>
</div>
</div> <!-- container end -->
</body>
<?php // READ CODE ?>
<?php
require("database.php");
$id = null;
if (!empty($_GET["id"])) {
$id = $_REQUEST["id"];
}
if (null == $id) {
header("Location: index.php");
} else {
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = "SELECT * FROM customers where id = ?";
$stmt = $pdo->prepare($query);
$stmt->bindValue(1, $id, PDO::PARAM_INT);
$stmt->execute();
$data = $stmt->fetch(PDO::FETCH_ASSOC);
Database::disconnect();
}
?>
<div class="span10 offset1">
<div class="row">
<h3>Read a Customer</h3>
</div>
<div class="form-horizontal">
<div class="control-group">
<label class="control-label">Name</label>
<div class="controls">
<label class="checkbox">
<?php echo $data["name"]; ?>
</label>
</div>
</div>
<div class="control-group">
<label class="control-label">Email Address</label>
<div class="controls">
<label class="checkbox">
<?php echo $data["email"] ?>
</label>
</div>
</div>
<div class="control-group">
<label class="control-label">Mobile Number</label>
<div class="controls">
<label class="checkbox">
<?php echo $data["mobile"] ?>
</label>
</div>
</div>
<div class="form-actions">
<a class="btn" href="index.php">Back</a>
</div>
</div>
</div>
</div> <!-- end of container -->
</body>
<?php // UPDATE CODE ?>
<?php
require("database.php");
$id = null;
if (!empty($_GET['id'])) {
$id = $_REQUEST["id"];
}
if (null == $id) {
header("Location: index.php");
}
if (!empty($_POST)) {
// Keep track of validation errors
$nameError = null;
$emailError = null;
$mobileError = null;
// Keep track post values
$name = $_POST["name"];
$email = $_POST["email"];
$mobile = $_POST["mobile"];
// Validate input
$valid = true;
if (empty($name)) {
$nameError = "Please enter Name";
$valid = false;
}
if (empty($email)) {
$emailError = "Please enter Email Address";
$valid = false;
} else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailError = "Please enter a valid Email Address";
$valid = false;
}
if (empty($mobile)) {
$mobileError = "Please enter Mobile Number";
$valid = false;
}
// Update data
if ($valid) {
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = "UPDATE customers set name = ?, email = ?, mobile = ? WHERE id = ?";
$stmt = $pdo->prepare($query);
$stmt->bindValue(1, $name, PDO::PARAM_STR);
$stmt->bindValue(2, $email, PDO::PARAM_STR);
$stmt->bindValue(3, $mobile, PDO::PARAM_INT);
$stmt->bindValue(4, $id, PDO::PARAM_INT);
$stmt->execute();
Database::disconnect();
header("Location: index.php");
}
} else { // For fields that are empty use the default values
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = "SELECT * FROM customers WHERE id = ?";
$stmt = $pdo->prepare($query);
$stmt->bindValue(1, $id, PDO::PARAM_INT);
$stmt->execute();
$data = $stmt->fetch(PDO::FETCH_ASSOC);
$name = $data["name"];
$email = $data["email"];
$mobile = $data["mobile"];
Database::disconnect();
}
?>
<div class="span10 offset1">
<div class="row">
<h3>Update a customer</h3>
</div>
<form class="form-horizontal" action="update.php?id=<?php echo $id ?>" method="post">
<div class="control-group <?php echo !empty($nameError) ? "error" : ""; ?>">
<label class="control-label">Name</label>
<div class="controls">
<input type="text" name="name" placeholder="Name" value="<?php echo !empty($name) ? $name : ""; ?>">
<?php if (!empty($nameError)): ?>
<span class="help-inline"><?php echo $nameError; ?></span>
<?php endif; ?>
</div>
</div>
<div class="control-group <?php echo !empty($emailError) ? "error" : ""; ?>">
<label class="control-label">Email Address</label>
<div class="controls">
<input type="text" name="email" placeholder="Email Address" value="<?php echo !empty($email) ? $email : ""; ?>" />
<?php if (!empty($emailError)): ?>
<span class="help-inline"><?php echo $emailError; ?></span>
<?php endif; ?>
</div>
</div>
<div class="control-group <?php echo !empty($mobileError) ? "error" : ""; ?>">
<label class="control-label">Mobile Number</label>
<div class="controls">
<input type="text" name="mobile" placeholder="Mobile Number" value="<?php echo !empty($mobile) ? $mobile : ""; ?>" />
<?php if (!empty($mobileError)): ?>
<span class="help-inline"><?php echo $mobileError; ?></span>
<?php endif; ?>
</div>
</div>
<div class="form actions">
<button type="submit" class="btn btn-success">Update</button>
<a class="btn" href="index.php">Back</a>
</div>
</form>
</div>
</div> <!-- container end -->
</body>
<?php // DELETE CODE ?>
<?php
require("database.php");
$id = 0;
if (!empty($_GET["id"])) {
$id = $_REQUEST["id"];
}
if (!empty($_POST)) {
// Keep track of post values
$id = $_POST["id"];
// Delete data
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = "DELETE FROM customers WHERE id = ?";
$stmt = $pdo->prepare($query);
$stmt->bindValue(1, $id, PDO::PARAM_INT);
$stmt->execute();
Database::disconnect();
header("Location: index.php");
}
?>
<div class="span10 offset1">
<div class="row">
<h3>Delete a Customer</h3>
</div>
<form class="form-horizontal" action="delete.php" method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>" />
<p class="alert alert-error">Are you sure you want to delete?</p>
<div class="form-actions">
<button type="submit" class="btn btn-danger">Yes</button>
<a class="btn" href="index.php">No</a>
</div>
</form>
</div>
</div> <!-- container end -->
</body>
[/php]