PHP MySQL CRUD Application
In this tutorial, we'll learn crud operations in PHP application with PHP and MySQL.
CRUD full form is Create, Read, Update, and Delete. CRUD operations are basic data manipulation for the database. how to crud operations in PHP perform create, read, update, and delete operations.
In this tutorial, we will learn how to make simple crud in PHP and MySQL application to perform all these operations in MySQL database table in one place.
let's start we'll use in all of our examples.
First afoul, we will create a database. How to make simple crud in PHP and MySQL.
CREATE TABLE users( id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, name VARCHAR(100) NOT NULL, email VARCHAR(255) NOT NULL );
When we create the database, we will need a config page. On this page, we will make a connection to the database.
database.php
<?php $conn = mysqli_connect('localhost','root','','userdata')or die('DB error');?>
In this step Insert query in PHP. Create create.php see below
create.php
<?php include('database.php')?> <html> <head> <title>PHP Coding Stuff</title> </head> <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <!-- Optional theme --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css"> <!-- Latest compiled and minified JavaScript --> <body> <form method="post"> <div class="container"> <div class="row"> <div class="col-md-3"></div> <div class="col-md-6"> <div class="form-group"> <label>Name</label> <input type="text" name="name" required placeholder="name"> </div> <div class="form-group"> <label>Email</label> <input type="email" name="email" required placeholder="email"> </div> <input type="submit" name="submit" value="Add User"> </form> </div> </div> </div> <script src="https://code.jquery.com/jquery-3.4.1.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> </body> </html> <?php if (isset($_POST['submit'])) { $name= $_POST['name']; $email= $_POST['email']; if ($name== '' OR $email == '') { ?> <script>alert('All Fild Are Required');</script> <?php }else{ $query="insert into users(name,email) values('$name','$email')"; mysqli_query($conn,$query); ?> <script>alert('User Added Successfully');</script> <?php } ?>
In this step fetch query in PHP. Create index.php see below
index.php
<?php include('database.php')?> <html> <head> <title>PHP Coding Stuff</title> </head> <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <!-- Optional theme --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css"> <!-- Latest compiled and minified JavaScript --> <body> <div class="container-fluid"> <div class="row"> <h2 style="background:blue; color:white;">Crud Opration</h2> </div> </div> <!--next--> <div class="container"> <a href="create.php" class="btn btn-success">Add Post</a> </div> <!--next--> <table class="table table-hover"> <tr> <th>S no.</th> <th>Name</th> <th>Email</th> <th>Action</th> </tr> <!--loop--> <?php $s = 1; $sql = "SELECT * FROM `users`"; $result=mysqli_query($conn,$sql); while($row=mysqli_fetch_array($result)){ ?> <tr> <td><?php echo $s++;?></td> <td><?php echo $row['name'];?></td> <td><?php echo $row['email'];?></td> <td><a href="edit.php?id=<?php echo $row['id'];?>" class="btn btn-warning">Edit</a> <a href="delete.php?id=<?php echo $row['id'];?>" class="btn btn-danger">Delete</a></td> </tr> <?php } ?> </table> <script src="https://code.jquery.com/jquery-3.4.1.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> </body> </html>
In this step update query in PHP. Create update.php see below
update.php
<?php include('database.php')?> <?php $how = $_GET['id']; $sql = "SELECT * FROM `users` WHERE id='$how'"; $result=mysqli_query($conn,$sql); $row=mysqli_fetch_array($result); ?> <html> <head> <title>PHP Coding Stuff</title> </head> <body> <form method="post"> <div class="container"> <div class="row"> <div class="col-md-3"></div> <div class="col-md-6"> <div class="form-group"> <label>Name</label> <input type="text" name="name" required placeholder="Name" value="<?php echo $row['name'];?>"> </div> <div class="form-group"> <label>Email</label> <input type="email" name="email" required placeholder="Email" value="<?php echo $row['email'];?>"> </div> <input type="submit" name="update" value="Update"> </form> </div> </div> </div> </body> </html> <?php if (isset($_POST['update'])) { $name= $_POST['name']; $email= $_POST['email']; if ($name== '' OR $email == '') { ?> <script>alert('All Fild Are Required');</script> <?php }else{ $sql = "UPDATE `user_list2` SET `title`='$title',`description`='$description' WHERE id='$how'"; mysqli_query($conn,$sql); ?> <script>alert('User Update Successfully');</script> <?php } ?>
This is the last step of delete, in this, we will learn delete query in PHP. so we'll create a delete.php see below
delete.php
<?php include('database.php')?> <?php $go = $_GET['id']; $sql = "DELETE FROM `users` WHERE id='$go'"; mysqli_query($conn,$sql); header('Location: ' . $_SERVER['HTTP_REFERER']); ?>
I hope it can help you...
Leave a Reply