Today, We want to share with you crud operations in php.In this post we will show you Insert, View, Edit and Delete Record from Database Using PHP, hear for crud operation in php source code download.
PHP Simple CRUD Application Script
we will give you demo and example for implement.In this post, we will learn about PHP Crud Mysqli Select Insert Update And Delete Query with an example.
Step 1: config.php
<?php /** * using mysqli_connect for database connection */ $databaseHost = 'localhost'; $databaseName = 'members_list'; $databaseUsername = 'root'; $databasePassword = ''; $mysqli = mysqli_connect($databaseHost, $databaseUsername, $databasePassword, $databaseName); ?>
Step 2: database.sql
create database members_list; use members_list; CREATE TABLE `members` ( `id` int(11) NOT NULL auto_increment, `name` varchar(100), `mail_address` varchar(100), `cell_phone` varchar(15), PRIMARY KEY (`id`) );
Step 3: index.php
<?php include_once("config.php"); $result = mysqli_query($mysqli, "SELECT * FROM members ORDER BY id DESC"); ?> <html> <head> <title>Welcome To Pakainfo.com</title> </head> <body> <a href="add.php">Add New Member</a><br/><br/> <table width='80%' border=1> <tr> <th>Name</th> <th>Mobile</th> <th>Email</th> <th>Update</th> </tr> <?php while($member_data = mysqli_fetch_array($result)) { echo "<tr>"; echo "<td>".$member_data['name']."</td>"; echo "<td>".$member_data['cell_phone']."</td>"; echo "<td>".$member_data['mail_address']."</td>"; echo "<td><a href='edit.php?id=$member_data[id]'>Edit</a> | <a href='delete.php?id=$member_data[id]'>Delete</a></td></tr>"; } ?> </table> </body> </html>
Step 4: add.php
<html> <head> <title>Add Members</title> </head> <body> <a href="index.php">Go to Home</a> <br/><br/> <form action="add.php" method="post" name="form1"> <table width="25%" border="0"> <tr> <td>Member Name</td> <td><input type="text" name="name"></td> </tr> <tr> <td>Member Email Address</td> <td><input type="text" name="mail_address"></td> </tr> <tr> <td>Member Cell Phone</td> <td><input type="text" name="cell_phone"></td> </tr> <tr> <td></td> <td><input type="submit" name="Submit" value="Add"></td> </tr> </table> </form> <?php if(isset($_POST['Submit'])) { $name = $_POST['name']; $mail_address = $_POST['mail_address']; $cell_phone = $_POST['cell_phone']; include_once("config.php"); $result = mysqli_query($mysqli, "INSERT INTO members(name,mail_address,cell_phone) VALUES('$name','$mail_address','$cell_phone')"); echo "Member added successfully. <a href='index.php'>View Members</a>"; } ?> </body> </html>
Step 5: edit.php
<?php include_once("config.php"); if(isset($_POST['update'])) { $id = $_POST['id']; $name=$_POST['name']; $cell_phone=$_POST['cell_phone']; $mail_address=$_POST['mail_address']; $result = mysqli_query($mysqli, "UPDATE members SET name='$name',mail_address='$mail_address',cell_phone='$cell_phone' WHERE id=$id"); header("Location: index.php"); } ?> <?php $id = $_GET['id']; $result = mysqli_query($mysqli, "SELECT * FROM members WHERE id=$id"); while($member_data = mysqli_fetch_array($result)) { $name = $member_data['name']; $mail_address = $member_data['mail_address']; $cell_phone = $member_data['cell_phone']; } ?> <html> <head> <title>Edit Member Data</title> </head> <body> <a href="index.php">Home</a> <br/><br/> <form name="update_member" method="post" action="edit.php"> <table border="0"> <tr> <td>Name</td> <td><input type="text" name="name" value=<?php echo $name;?>></td> </tr> <tr> <td>Email</td> <td><input type="text" name="mail_address" value=<?php echo $mail_address;?>></td> </tr> <tr> <td>Cell Phone</td> <td><input type="text" name="cell_phone" value=<?php echo $cell_phone;?>></td> </tr> <tr> <td><input type="hidden" name="id" value=<?php echo $_GET['id'];?>></td> <td><input type="submit" name="update" value="Update"></td> </tr> </table> </form> </body> </html>
Step 6: delete.php
<?php include_once("config.php"); $id = $_GET['id']; $result = mysqli_query($mysqli, "DELETE FROM members WHERE id=$id"); header("Location:index.php"); ?>
I hope you get an idea about crud operations in php.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.