In this tutorial we will create a
Emptying Table In MySQLi using PHP. This code can empty your table when the user click the empty button button.The code use MySQLi
TRUNCATE TABLE query to empty your existing table instantly in the MySQLi database. This a user-friendly program feel free to modify and use it to your system.
We will be using
PHP as a scripting language and interpreter that is used primarily on any webserver including xamp, wamp, etc. It is being use to any famous websites because of modern approach as its today.
Getting Started:
First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server
https://www.apachefriends.org/index.html.
And, this is the link for the bootstrap that i used for the layout design
https://getbootstrap.com/.
Creating Database
Open your database web server then create a database name in it
db_empty_table, after that click Import then locate the database file inside the folder of the application then click ok.
Creating the database connection
Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it
conn.php.
<?php
if(!$conn){
die("Error: Failed to connect to database!"); }
?>
Creating The Interface
This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as
index.php.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" name="viewport" content="width=device-width, intial-scale=1"/>
<link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
</head>
<body>
<nav class="navabr navbar-default">
<div class="container-fluid">
<a class="navbar-brand" href="https://sourcecodeter.com">Sourceodester</a>
</div>
</nav>
<div class="col-md-3"></div>
<div class="col-md-6 well">
<h3 class="text-primary">PHP - Emptying Table In MySQLi</h3>
<hr style="border-top:1px dotted #ccc;"/>
<div class="col-md-4">
<form method="POST" action="insert.php">
<div class="form-group">
<label>Firstname</label>
<input type="text" name="firstname" class="form-control" required="required"/>
</div>
<div class="form-group">
<label>Lastbane</label>
<input type="text" name="lastname" class="form-control" required="required"/>
</div>
<div class="form-group">
<label>Address</label>
<input type="text" name="address" class="form-control" required="required"/>
</div>
<center><button class="btn btn-primary" name="insert">Insert</button></center>
</form>
</div>
<div class="col-md-8">
<a class="btn btn-danger" href="empty.php">Empty Table</a>
<br /><br />
<table class="table table-bordered">
<thead class="alert-info">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<?php
require'conn.php';
echo"<tr>
<td>".$fetch['firstname']."</td>
<td>".$fetch['lastname']."</td>
<td>".$fetch['address']."</td>
</tr>";
}
?>
</tbody>
</table>
</div>
</div>
</body>
</html>
Creating PHP Query
This code contains the php query of the application. This code will store the data inputs to the database server. To do that just copy and write this block of codes inside the text editor, then save it as
insert.php.
<?php
require_once'conn.php';
if(ISSET($_POST['insert'])){ $firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$address=$_POST['address'];
header("location: index.php"); }
?>
Creating the Main Function
This code contains the main function of the application. This code will empty your table data when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as
empty.php
<?php
require_once'conn.php';
header("location: index.php"); ?>
There you have it we successfully created
Emptying Table In MySQLi using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!