|
CRUD is an acronym for create, read, update and delete. In this lesson we will learn how to create a CRUD in PHP and MYSQLI and also how to search a record from a database. We will also learn a bit about HTML and CSS as well.
If you would like to learn CRUD in visual basic.net using store procedure, then you can read that from CRUD in visual basic.net.
In this CRUD in PHP and MYSQLI we will see how to:
- Connect to Database
- Add new data to Database
- Select data from Database
- Edit record by id
- Delete record from the database
- Search records from the database
index.php
<?php
include('conn.php');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CRUD using PHP & MYSQL</title>
<style>
#wrapper{
width:960px;
margin:0 auto;
background-color: #e3e3e3;
text-align: center;
}
#search{
background-color: orange;
height: 40px;
}
#search input[type="text"]{
height: 25px;
width:400px;
margin-top: 5px;
outline: none;
}
#search input[type="submit"]{
background-color: #333;
color:#fff;
height: 32px;
width:100px;
margin-top: 5px;
}
table{
text-align: center;
}
table th{
background-color: #900;
color: #fff;
padding:0px 54px;
height: 30px;
}
table tr{
text-align: center;
}
</style>
</head>
<body>
<div id="wrapper">
<h1>CRUD using PHP & MYSQL</h1>
<form action="index.php" method="POST">
<p><label for="name">Please enter your name:</label></p>
<p><input type="text" name="name"></p>
<p><label for="address">Please enter your Address:</label></p>
<p><input type="text" name="address"></p>
<p><label for="email">Please enter your email</label></p>
<p><input type="email" name="email"></p>
<p><input type="submit" name="insert" value="Save Record"></p>
</form>
<?php
if(isset($_POST['insert'])){
$name=$_POST['name'];
$address=$_POST['address'];
$email=$_POST['email'];
$query="insert into students (st_name,address,email) values ('$name','$address','$email')";
$row=mysqli_query($con,$query);
if($row){
echo'<script>alert("Record inserted successfully")</script>';
}
}
?>
<div id="search">
<form action="search.php" method="GET">
<input type="text" name="search" placeholder=" search...">
<input type="submit" value="search">
</form>
</div>
<div id="records">
<h2>Records in the database table</h2>
<table>
<tr>
<th>ID</th>
<th>NAME</th>
<th>ADDRESS</th>
<th>EMAIL</th>
<th>EDIT</th>
<th>DELETE</th>
</tr>
<?php
$query="select * from students";
$result=mysqli_query($con,$query);
while ($row=mysqli_fetch_assoc($result)) {
$id=$row['ID'];
$name=$row['st_name'];
$address=$row['address'];
$email=$row['email'];
?>
<tr>
<td><?php echo $id;?></td>
<td><?php echo $name;?></td>
<td><?php echo $address;?></td>
<td><?php echo $email;?></td>
<td><a href="edit.php?edit=<?php echo $id ;?>">Edit</a></td>
<td><a href="delete.php?del=<?php echo $id ;?>">Delete</a></td>
</tr>
<?php }?>
</table>
</div>
</div>
</body>
</html>
conn.php
<?php
$con=mysqli_connect('localhost','root','','youtube');
?>
edit.php
<?php
include('conn.php');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CRUD using PHP & MYSQL</title>
<style>
#wrapper{
width:960px;
margin:0 auto;
background-color: #e3e3e3;
text-align: center;
}
#search{
background-color: orange;
height: 40px;
}
#search input[type="text"]{
height: 25px;
width:400px;
margin-top: 5px;
outline: none;
}
#search input[type="submit"]{
background-color: #333;
color:#fff;
height: 32px;
width:100px;
margin-top: 5px;
}
table{
text-align: center;
}
table th{
background-color: #900;
color: #fff;
padding:0px 54px;
height: 30px;
}
table tr{
text-align: center;
}
</style>
</head>
<body>
<div id="wrapper">
<h1>CRUD using PHP & MYSQL</h1>
<?php
$edit_id=$_GET['edit'];
$query="select * from students where id='$edit_id'";
$run=mysqli_query($con,$query);
while ($row=mysqli_fetch_assoc($run)) {
$id=$row['ID'];
$name=$row['st_name'];
$address=$row['address'];
$email=$row['email'];
?>
<form action="edit.php?edit_form=<?php echo $id;?>" method="POST">
<p><label for="name">Please enter your name:</label></p>
<p><input type="text" name="name" value=<?php echo $name;?>></p>
<p><label for="address">Please enter your Address:</label></p>
<p><input type="text" name="address" value=<?php echo $address;?>></p>
<p><label for="email">Please enter your email</label></p>
<p><input type="email" name="email" value=<?php echo $email;?>></p>
<p><input type="submit" name="update" value="Update Record"></p>
</form>
<?php }?>
</div>
</body>
</html>
<?php
if(isset($_POST['update'])){
$update_id=$_GET['edit_form'];
$name=$_POST['name'];
$address=$_POST['address'];
$email=$_POST['email'];
$query="update students set st_name='$name',address='$address',email='$email' where id='$update_id'";
$row=mysqli_query($con,$query);
if($row){
echo '<script>alert("Updated successfully")</script>';
header('Location:index.php');
}
}
?>
delete.php
<?php
include('conn.php');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CRUD using PHP & MYSQL</title>
<style>
#wrapper{
width:960px;
margin:0 auto;
background-color: #e3e3e3;
text-align: center;
}
#search{
background-color: orange;
height: 40px;
}
#search input[type="text"]{
height: 25px;
width:400px;
margin-top: 5px;
outline: none;
}
#search input[type="submit"]{
background-color: #333;
color:#fff;
height: 32px;
width:100px;
margin-top: 5px;
}
table{
text-align: center;
}
table th{
background-color: #900;
color: #fff;
padding:0px 54px;
height: 30px;
}
table tr{
text-align: center;
}
</style>
</head>
<body>
<div id="wrapper">
<h1>CRUD using PHP & MYSQL</h1>
<?php
$del_id=$_GET['del'];
$query="delete from students where id='$del_id'";
if(mysqli_query($con,$query)){
header('LOCATION:index.php');
}
?>
<form action="edit.php?edit_form=<?php echo $id;?>" method="POST">
<p><label for="name">Please enter your name:</label></p>
<p><input type="text" name="name" value=<?php echo $name;?>></p>
<p><label for="address">Please enter your Address:</label></p>
<p><input type="text" name="address" value=<?php echo $address;?>></p>
<p><label for="email">Please enter your email</label></p>
<p><input type="email" name="email" value=<?php echo $email;?>></p>
<p><input type="submit" name="update" value="Update Record"></p>
</form>
</div>
</body>
</html>
search.php
You can also check the video on my youtube channel.
<?php
include('conn.php');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CRUD using PHP & MYSQL</title>
<style>
#wrapper{
width:960px;
margin:0 auto;
background-color: #e3e3e3;
text-align: center;
}
#search{
background-color: orange;
height: 40px;
}
#search input[type="text"]{
height: 25px;
width:400px;
margin-top: 5px;
outline: none;
}
#search input[type="submit"]{
background-color: #333;
color:#fff;
height: 32px;
width:100px;
margin-top: 5px;
}
table{
text-align: center;
}
table th{
background-color: #900;
color: #fff;
padding:0px 94px;
height: 30px;
}
table tr{
text-align: center;
}
</style>
</head>
<body>
<div id="wrapper">
<h1>CRUD using PHP & MYSQL</h1>
<div id="records">
<h2>Records in the database table</h2>
<table>
<tr>
<th>ID</th>
<th>NAME</th>
<th>ADDRESS</th>
<th>EMAIL</th>
</tr>
<?php
if(isset($_GET['search'])){
$search_name=$_GET['search'];
$query="select * from students where st_name LIKE '$search_name%'";
$result=mysqli_query($con,$query);
while ($row=mysqli_fetch_assoc($result)) {
$id=$row['ID'];
$name=$row['st_name'];
$address=$row['address'];
$email=$row['email'];
?>
<tr>
<td><?php echo $id;?></td>
<td><?php echo $name;?></td>
<td><?php echo $address;?></td>
<td><?php echo $email;?></td>
<?php }}?>
</tr>
<tr><td colspan="4"><a href="index.php">Home</a></td></tr>
</table>
</div>
</div>
</body>
</html>
0 comments:
Post a Comment