,

Add Edit and Delete Records with Basic PHP and MySQL


First, prepare your database. 
CREATE DATABASE `ks_add_edit_delete`;


In my case, i named my database as ks_add_edit_delete and 
my table as info.
CREATE TABLE `info` (
`id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Next, Create your configuration file to connect in your database. 

Create a new file and name it as config.php
#Image Creating a config.php
Open config.php in your text editor and insert this code.
<?php
$host = 'localhost';
$user = 'root';
$pass = '';
$database = 'ks_add_edit_delete';
$conn = mysqli_connect($host,$user,$pass,$database);
if (!$conn){
die("DB Handler Connection Failed: " . mysqli_connect_error());
}
view raw config.php hosted with ❤ by GitHub

Next is to create your index.php in your host directory and copy the following codes.
<?php
//REQUIRE CONFIG FOR DB HANDLING
require 'config.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="icon" href="../assets/images/favicon.ico">
<title>Add Edit Delete Simple PHP and MySQL | KSolutions PH</title>
</head>
<body>
<h1 >INFORMATION TABLE</h1>
<a href="add.php">Add New</a>
<br><br>
<table border="1" style="border-collapse: collapse;width: 600px;">
<thead style="background-color: teal;color: white;">
<tr>
<th>ID</th>
<th>NAME</th>
<th>EMAIL</th>
<th>ACTION</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</body>
</html>
view raw index.php hosted with ❤ by GitHub

Do not forget to require the config file you created earlier.

Create html tags and insert <h1> tags inside the <body> as a page title reference.
<a> tag is also added that is pointing to add.php that you will be creating later.

After it, Create a table where you want to display your records.
Place the following code below to fetch all the records from the database and display it on the table body.
<?php
$sql = "SELECT * FROM info";
$result = mysqli_query($conn,$sql);
while ($row = mysqli_fetch_assoc($result)){
?>
<tr>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['email'];?></td>
<td>
<a href="edit.php?id=<?php echo $row['id'] ?>">Edit</a>
<a href="delete.php?id=<?php echo $row['id'] ?>" onclick="return confirm('Are you sure to continue?');">Delete</a>
</td>
</tr>
<?php
}
?>
view raw index.php hosted with ❤ by GitHub

Next is for adding a new record. You need to create a new page and name it to add.php.
<?php
//REQUIRE CONFIG FOR DB HANDLING
require 'config.php';
$summary = "";
$name = $email = "";
$nameErr = $emailErr = "";
//VALIDATION NEEDS TO BE DONE HERE
?>
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="icon" href="../assets/images/favicon.ico">
<title>Add Edit Delete Simple PHP and MySQL | KSolutions PH</title>
</head>
<style type="text/css">
.error{
color: red;
}
.success{
color: green;
}
</style>
<body>
<h1>ADD NEW</h1>
<a href="index.php">Home</a>
<br><br>
<form method="post">
<div class="success"><?php echo $summary;?></div>
<label>NAME : </label><br>
<input type="text" name="name" value="<?php echo $name; ?>"><br>
<div class="error"><?php echo $nameErr;?></div>
<label>EMAIL : </label><br>
<input type="email" name="email" value="<?php echo $email; ?>"><br>
<div class="error"><?php echo $emailErr;?></div><br>
<button type="submit" name="btn-submit"> Save </button>
</form>
</body>
</html>
view raw add.php hosted with ❤ by GitHub

Create a form with your fields in the add.php

Validation needs to be done after the declaration of the variables that we will be needing in the result summary or errors.
if (isset($_POST['btn-submit'])) {
$name = mysqli_escape_string($conn,$_POST['name']);
$email = mysqli_escape_string($conn,$_POST['email']);
if (empty($name)) {
$nameErr = "This field is required.";
}
if (empty($email)) {
$emailErr = "This field is required.";
}
if (!$nameErr && !$emailErr) {
$sql = "INSERT INTO info(name,email) VALUES('{$name}','{$email}')";
if (mysqli_query($conn,$sql)) {
$summary = "Data Saved.";
}
}
}
view raw add.php hosted with ❤ by GitHub

Next is the edit.php, It is like the add.php but you need the id of the record in order to fetch the data stored in your database.
<?php
//REQUIRE CONFIG FOR DB HANDLING
require 'config.php';
$summary = "";
$name = $email = "";
$nameErr = $emailErr = "";
if (isset($_GET['id']) && $_GET['id'] !== '') {
$id = $_GET['id'];
$sql = "SELECT * FROM info WHERE id='{$id}'";
$result = mysqli_query($conn,$sql);
while ($row = mysqli_fetch_assoc($result)) {
$name = $row['name'];
$email = $row['email'];
}
}
if (isset($_POST['btn-submit'])) {
$name = mysqli_escape_string($conn,$_POST['name']);
$email = mysqli_escape_string($conn,$_POST['email']);
if (empty($name)) {
$nameErr = "This field is required.";
}
if (empty($email)) {
$emailErr = "This field is required.";
}
if (!$nameErr && !$emailErr) {
$sql = "UPDATE info SET name='{$name}',email='{$email}' WHERE id='{$id}'";
if (mysqli_query($conn,$sql)) {
$summary = "Data Updated.";
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="icon" href="../assets/images/favicon.ico">
<title>Add Edit Delete Simple PHP and MySQL | KSolutions PH</title>
</head>
<style type="text/css">
.error{
color: red;
}
.success{
color: green;
}
</style>
<body>
<h1>EDIT</h1>
<a href="index.php">Home</a>
<br><br>
<form method="post">
<div class="success"><?php echo $summary;?></div>
<label>NAME : </label><br>
<input type="text" name="name" value="<?php echo $name; ?>"><br>
<div class="error"><?php echo $nameErr;?></div>
<label>EMAIL : </label><br>
<input type="email" name="email" value="<?php echo $email; ?>"><br>
<div class="error"><?php echo $emailErr;?></div><br>
<button type="submit" name="btn-submit"> Update </button>
</form>
</body>
</html>
view raw edit.php hosted with ❤ by GitHub

Lastly is delete.php . Get the id of the record and delete the row where the value of the id matches.
<?php
//REQUIRE CONFIG FOR DB HANDLING
require 'config.php';
$summary = "";
$name = $email = "";
$nameErr = $emailErr = "";
if (isset($_GET['id']) && $_GET['id'] !== '') {
$id = $_GET['id'];
$sql = "DELETE FROM info WHERE id='{$id}'";
if (mysqli_query($conn,$sql)) {
?>
<script>
alert('Successfully Deleted!');
</script>
<?php
}
}
?>
<script>
window.location = 'index.php';
</script>
view raw delete.php hosted with ❤ by GitHub

Your final output should look like the following.







You can download the full source code below.

SOURCE CODE
1. Download the source code & place it on your host.
2. Create a database ‘ks_add_edit_delete’.
3. Import the sql file located at the source code's folder.
4. Test it on your host.

DOWNLOAD IT HERE -> DOWNLOAD

Enjoy !
Please share this if you find it helpful. Thank you.
Share:

Related Posts:

No comments:

Post a Comment