0% found this document useful (0 votes)
20 views

StarTutorial - PHP Crud Tutorial Part 2

This document discusses creating the create and read pages for a PHP CRUD application. It shows how to add create and read buttons to the index page. It also covers creating a create page with an HTML form to enter new records.

Uploaded by

Anselmo Nicoski
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

StarTutorial - PHP Crud Tutorial Part 2

This document discusses creating the create and read pages for a PHP CRUD application. It shows how to add create and read buttons to the index page. It also covers creating a create page with an HTML form to enter new records.

Uploaded by

Anselmo Nicoski
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

11/12/2021 01:28 StarTutorial: Php Crud Tutorial Part 2

Home Latest Series Free eBooks Membership

Share on Facebook Share on Twitter Email

PHP CRUD Tutorial (part


2)
PHP CRUD Tutorial (part 1)

PHP CRUD Tutorial (part 2)

PHP CRUD Tutorial (part 3)

Advanced PHP CRUD Tutorial

In this part of tutorial, we are going to create "Create" and "Read" pages of
PHP CRUD grid.

Adding "Create" button and "Read" button

Free eBook: Modern PHP Developer


https://startutorial.com/view/php-crud-tutorial-part-2 1/16
11/12/2021 01:28 StarTutorial: Php Crud Tutorial Part 2

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

Creating a "Create" page Free eBook: Modern PHP Developer


https://startutorial.com/view/php-crud-tutorial-part-2 4/16
11/12/2021 01:28 StarTutorial: Php Crud Tutorial Part 2

Create a PHP file "create.php"; This file contains an html form and it is the
"Create" part of CRUD grid.

We will go through this file as two parts.

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:

Free eBook: Modern PHP


<!DOCTYPE html> Developer
<html lang="en"> Download ebook Modern PHP Developer for free.
<head>
<meta charset="utf-8"> Email Address

<link href="css/bootstrap.min.css" rel="stylesheet">


Submit
<script src="js/bootstrap.min.js"></script>
</head>

<body>
<div class="container">

<div class="span10 offset1">


<div class="row">
Free eBook: Modern PHP Developer
<h3>Create a Customer</h3>
https://startutorial.com/view/php-crud-tutorial-part-2 5/16
11/12/2021 01:28 StarTutorial: Php Crud Tutorial Part 2

</div>

<form class="form-horizontal" action="create.php"


<div class="control-group <?php echo !empty($na
<label class="control-label">Name</label>
<div class="controls">
<input name="name" type="text" placehold
<?php if (!empty($nameError)): ?>
<span class="help-inline"><?php echo
<?php endif; ?>
</div> Free eBook: Modern PHP
</div> Developer
<div class="control-group <?php echo !empty($em
Download ebook Modern PHP Developer for free.
<label class="control-label">Email Address</l
<div class="controls"> Email Address
<input name="email" type="text" placehold
<?php if (!empty($emailError)):
Submit ?>
<span class="help-inline"><?php echo
<?php endif;?>

</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

<?php if (!empty($mobileError)): ?>


<span class="help-inline"><?php echo
<?php endif;?>
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-succes
<a class="btn" href="index.php">Back</a>
</div>
</form>
</div> Free eBook: Modern PHP
Developer
</div> <!-- /container -->
Download ebook Modern PHP Developer for free.
</body>
</html> Email Address

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

require 'database.php'; Free eBook: Modern PHP Developer


https://startutorial.com/view/php-crud-tutorial-part-2 7/16
11/12/2021 01:28 StarTutorial: Php Crud Tutorial Part 2

if ( !empty($_POST)) {
// keep track validation errors
$nameError = null;
$emailError = null;
$mobileError = null;

// keep track post values

$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

$nameError = 'Please enter Name';


Submit
$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; 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:

Free eBook: Modern PHP


Developer
Download ebook Modern PHP Developer for free.

Email Address

Submit

Create some records by entering correct customer information. You should be


able to see a CRUD grid as below.

Free eBook: Modern PHP Developer


https://startutorial.com/view/php-crud-tutorial-part-2 10/16
11/12/2021 01:28 StarTutorial: Php Crud Tutorial Part 2

Free eBook: Modern PHP


You must have noticed a "Read" button for each row. That is what we have
Developer
added in step 1 of this tutorial. If you click it now, it will lead you to an error
page. And that is what we are going to create next step. Download ebook Modern PHP Developer for free.

Email Address

Creating a "Read" page Submit

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 class="span10 offset1">


<div class="row">
<h3>Read a Customer</h3>

</div>

<div class="form-horizontal" >


Free eBook: Modern PHP
<div class="control-group">
Developer
<label class="control-label">Name</label>
<div class="controls"> Download ebook Modern PHP Developer for free.
<label class="checkbox">
Email Address
<?php echo $data['name'];?>
</label>
Submit
</div>
</div>
<div class="control-group">
<label class="control-label">Email Address</l
<div class="controls">
<label class="checkbox">
<?php echo $data['email'];?>
</label>
Free eBook: Modern PHP Developer
</div>
https://startutorial.com/view/php-crud-tutorial-part-2 13/16
11/12/2021 01:28 StarTutorial: Php Crud Tutorial Part 2

</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

</div> <!-- /container -->


</body>
</html>

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.

If you get it working. "read.php" page should look like below:

Free eBook: Modern PHP


Developer
Download ebook Modern PHP Developer for free.

Email Address

Submit

To be continued

If you have followed along correctly. You should have some files as below:

Free eBook: Modern PHP Developer


https://startutorial.com/view/php-crud-tutorial-part-2 15/16
11/12/2021 01:28 StarTutorial: Php Crud Tutorial Part 2

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.

© 2021 Star Tutorial. All rights reserved.

Free eBook: Modern PHP Developer


https://startutorial.com/view/php-crud-tutorial-part-2 16/16

You might also like