NextIndexPrevious

Programs in PHP

25. A simple CRUD operations using PHP and Mysql.

crud.php

<html> <head> <script> var obj = new XMLHttpRequest(); obj.open("GET","read.php",true); obj.send(); obj.onreadystatechange = function(){ if(obj.readyState==4) { document.getElementById("data").innerHTML = obj.responseText; } } </script> </head> <body> <div id="header"> <h1>CRUD</h1> </div> <div id="nav"> <a href="crud.php">Home</a> | <a href="register.php">Register</a> </div> <h3>User details</h3> <div id="data"></div> </body> </html>

register.php

<html> <head> </head> <body> <div id="header"> <h1>CRUD</h1> </div> <div id="nav"> <a href="crud.php">Home</a> | <a href="register.php">Register</a> </div> <h3>Register here</h3> <div id="data"> <form action="insert.php" method="post"> <label>First name</label> <input type="text" name="fname"><br> <label>Last name</label> <input type="text" name="lname"><br> <label>City</label> <input type="text" name="city"><br> <input type="submit" value="Save"> </form> </div> </body> </html>

insert.php

<?php if(isset($_POST['fname'])) { $fname = $_POST['fname']; $lname = $_POST['lname']; $city = $_POST['city']; $conn = mysqli_connect("localhost","root","","cruddb"); if(!$conn) { echo "Connection failed!"; exit; } $sql = "insert into user(fname,lname,city,status) value('$fname','$lname','$city',1)"; if(mysqli_query($conn,$sql)) { echo "<script>alert('Registration successful');location.href='crud.php';</script>"; } else { echo mysqli_error($conn); } } ?>

read.php

<?php $conn = mysqli_connect("localhost","root","","cruddb"); if(!$conn) { echo "Connection failed!"; exit; } $sql = "select * from user where status=1"; $result = mysqli_query($conn,$sql); echo "<table border='1'>"; while($row = mysqli_fetch_assoc($result)) { echo "<tr>"; echo "<td>".$row['fname']."</td>"; echo "<td>".$row['lname']."</td>"; echo "<td>".$row['city']."</td>"; echo "<td><a href='edit.php?id=".$row['id']."'>Edit</a></td>"; echo "<td><a href='delete.php?id=".$row['id']."'>Delete</a></td>"; echo "</tr>"; } echo "</table>"; ?>

edit.php

<?php if(isset($_GET['id'])) { $id = $_GET['id']; $conn = mysqli_connect("localhost","root","","cruddb"); if(!$conn) { echo "Connection failed!"; exit; } $sql = "select * from user where id=$id and status=1"; $result = mysqli_query($conn,$sql); $row = mysqli_fetch_assoc($result); } ?> <html> <head> </head> <body> <div id="header"> <h1>CRUD</h1> </div> <div id="nav"> <a href="crud.php">Home</a> | <a href="register.php">Register</a> </div> <h3>Update user</h3> <div id="data"> <form action="update.php" method="post"> <label>First name</label> <input type="text" name="fname" value="<?php echo $row['fname']; ?>"><br> <label>Last name</label> <input type="text" name="lname" value="<?php echo $row['lname']; ?>"><br> <label>City</label> <input type="text" name="city" value="<?php echo $row['city']; ?>"><br> <input type="hidden" name="id" value="<?php echo $row['id']; ?>"> <input type="submit" value="Update"> </form> </div> </body> </html>

update.php

<?php if(isset($_POST['fname'])) { $fname = $_POST['fname']; $lname = $_POST['lname']; $city = $_POST['city']; $id = $_POST['id']; $conn = mysqli_connect("localhost","root","","cruddb"); if(!$conn) { echo "Connection failed!"; exit; } $sql = "update user set fname='$fname', lname='$lname', city='$city' where id=$id"; if(mysqli_query($conn,$sql)) { echo "<script>alert('Updation successful');location.href='crud.php';</script>"; } else { echo mysqli_error($conn); } } ?>

delete.php

<?php if(isset($_GET['id'])) { $id = $_GET['id']; $conn = mysqli_connect("localhost","root","","cruddb"); if(!$conn) { echo "Connection failed!"; exit; } $sql = "delete from user where id=$id"; if(mysqli_query($conn,$sql)) { echo "<script>alert('Deletion successful');location.href='crud.php';</script>"; } else { echo mysqli_error($conn); } } ?>

Father of PHP

Rasmus Lerdorf

Rasmus Lerdorf

Born: November 22, 1968, Greenland
Google+