StarTutorial - PHP Crud Tutorial Part 2
StarTutorial - PHP Crud Tutorial Part 2
In this part of tutorial, we are going to create "Create" and "Read" pages of
PHP CRUD grid.
To start, we firstly need a navigation button which leads to "Create" page and
also a button to read a record. Open "index.php" file we created in part 1 of
this tutorial. And add a "Create" button at the top of the table, and also "Read"
buttons for each row of the table.
Now the "index.php" file's code should look like below, the highlighted codes
are what we have added:
<!DOCTYPE html>
<html lang="en">
Free eBook: Modern PHP
<head> Developer
<meta charset="utf-8"> Download ebook Modern PHP Developer for free.
<link href="css/bootstrap.min.css" rel="stylesheet">
<script src="js/bootstrap.min.js"></script> Email Address
</head>
Submit
<body>
<div class="container">
<div class="row">
<h3>PHP CRUD Grid</h3>
</div>
<div class="row">
<p>
Free eBook: Modern PHP Developer
<a href="create.php" class="btn btn-success">Crea
https://startutorial.com/view/php-crud-tutorial-part-2 2/16
11/12/2021 01:28 StarTutorial: Php Crud Tutorial Part 2
</p>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Email Address</th>
<th>Mobile Number</th>
<th>Action</th>
</tr>
</thead>
<tbody> Free eBook: Modern PHP
<?php Developer
include 'database.php';
Download ebook Modern PHP Developer for free.
$pdo = Database::connect();
$sql = 'SELECT * FROM customers ORDER BY id DE
Email Address
foreach ($pdo->query($sql) as $row) {
echo '<tr>'; Submit
echo '<td>'. $row['name'] . '</td>';
echo '<td>'. $row['email'] . '</td>';
echo '<td>'. $row['mobile'] . '</td>'
echo '<td><a class="btn" href="read.p
echo '</tr>';
}
Database::disconnect();
?> Free eBook: Modern PHP Developer
https://startutorial.com/view/php-crud-tutorial-part-2 3/16
11/12/2021 01:28 StarTutorial: Php Crud Tutorial Part 2
</tbody>
</table>
</div>
</div> <!-- /container -->
</body>
</html>
Now if you navigate to "index.php page". You should notice a "Create" button.
Free eBook: Modern PHP
However since its linked page is not created, it will redirect to an error page if
Developer
you click the button. We will fix that next step:
Download ebook Modern PHP Developer for free.
Email Address
Submit
Create a PHP file "create.php"; This file contains an html form and it is the
"Create" part of CRUD grid.
First part of the codes is an html form. It is pretty straightforward; all it does is
to create an html form. As we need to provide validation for form entries.
Hence there is a PHP variable for holding validation error for each field. Copy
the code below to "create.php" file:
<body>
<div class="container">
</div>
</div>
</div>
<div class="control-group <?php echo !empty($mo
<label class="control-label">Mobile Number</l
<div class="controls">
<input name="mobile"Free eBook: Modern
type="text" PHP Developer
placeho
https://startutorial.com/view/php-crud-tutorial-part-2 6/16
11/12/2021 01:28 StarTutorial: Php Crud Tutorial Part 2
Submit
Second part of the codes is where the magic happens. It handles record
creation process. Copy the codes below to the beginning of "create.php" file,
we will go through them afterwards:
<?php
if ( !empty($_POST)) {
// keep track validation errors
$nameError = null;
$emailError = null;
$mobileError = null;
$name = $_POST['name'];
$email = $_POST['email'];
Free eBook: Modern PHP
$mobile = $_POST['mobile'];
Developer
// validate input Download ebook Modern PHP Developer for free.
$valid = true;
if (empty($name)) { Email Address
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; Free eBook: Modern PHP Developer
https://startutorial.com/view/php-crud-tutorial-part-2 8/16
11/12/2021 01:28 StarTutorial: Php Crud Tutorial Part 2
if (empty($mobile)) {
$mobileError = 'Please enter Mobile Number';
$valid = false;
}
// insert data
if ($valid) {
$pdo = Database::connect();
Free eBook: Modern PHP
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPT
$sql = "INSERT INTO customers
Developer
(name,email,mobile) values(
$q = $pdo->prepare($sql); Download ebook Modern PHP Developer for free.
$q->execute(array($name,$email,$mobile));
Email Address
Database::disconnect();
header("Location: index.php");
Submit
}
}
?>
As you see, firstly we check if there is form submit by checking $_POST variable.
If so, we check each entries to ensure they are not empty. Additionally for email
address entry, we use PHP filter to verify if it is a valid email
Freeaddress. Then if PHP
eBook: Modern it Developer
https://startutorial.com/view/php-crud-tutorial-part-2 9/16
11/12/2021 01:28 StarTutorial: Php Crud Tutorial Part 2
passes all validation rules, it inserts data to database using Database class. At
last it will redirect to "index.php" using PHP header() function. However if there
is any validation error, the validation variables will be showed in the form.
If you have followed correctly. Navigate to "create.php" page, and click on the
"Create" button, you should be able to see a PHP form with validation errors as
below:
Email Address
Submit
Email Address
Create a PHP file "read.php"; this file is the "Read" part of CRUD grid.
Comparing to "Create" part, this step is pretty straightforward. Copy codes
below to the file and we will explain afterwards.
<?php
require 'database.php'; Free eBook: Modern PHP Developer
https://startutorial.com/view/php-crud-tutorial-part-2 11/16
11/12/2021 01:28 StarTutorial: Php Crud Tutorial Part 2
$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)
Free eBook: Modern PHP
$sql = "SELECT * FROM customers where id = ?";
Developer
$q = $pdo->prepare($sql);
$q->execute(array($id)); Download ebook Modern PHP Developer for free.
$data = $q->fetch(PDO::FETCH_ASSOC);
Email Address
Database::disconnect();
}
Submit
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link href="css/bootstrap.min.css" rel="stylesheet">
<script src="js/bootstrap.min.js"></script>
Free eBook: Modern PHP Developer
</head>
https://startutorial.com/view/php-crud-tutorial-part-2 12/16
11/12/2021 01:28 StarTutorial: Php Crud Tutorial Part 2
<body>
<div class="container">
</div>
</div>
<div class="control-group">
<label class="control-label">Mobile Number</l
<div class="controls">
<label class="checkbox">
<?php echo $data['mobile'];?>
</label>
</div>
</div>
<div class="form-actions">
Free eBook: Modern PHP
<a class="btn" href="index.php">Back</a>
Developer
</div>
Download ebook Modern PHP Developer for free.
Email Address
</div>
</div>
Submit
Firstly let us look at the beginning part of the PHP codes. What it does is that, it
tries to allocate a $_GET['id'] variable. If it is not found, itFree
redirects
eBook:toModern PHP Developer
https://startutorial.com/view/php-crud-tutorial-part-2 14/16
11/12/2021 01:28 StarTutorial: Php Crud Tutorial Part 2
"index.php" page. Otherwise, it will read data from database using the "id" field
and store data into a PHP variable $data.
Next part, which is the static html part, is pretty simple. It prints out the $data
variable.
Email Address
Submit
To be continued
If you have followed along correctly. You should have some files as below:
In next tutorial (PHP CRUD tutorial part 3), We are going to create the
"Update" and "Delete" pages of PHP CRUD grid. Make sure you bookmark this
Free
page and come back to visit us next week. We will provide eBook:
source code Modern PHP
Developer
package at the last part of this series of tutorial as well. And also if you enjoy
this tutorial, please share them using the share buttons on left side
Download of Modern
ebook the post.
PHP Developer for free.
Email Address
Hopefully this simple tutorial helped you with your development. If you like our
post, please follow us on Twitter and help spread the word. We need your
Submit
support to continue. If you have questions or find our mistakes in above
tutorial, do leave a comment below to let us know.