Basic PHP CRUD Application
Simple PHP and MySQL application for performing crud operations.
It is a simple core PHP application which performs all the crud operations.
What is CRUD?
CRUD is stands for Create Read Update and Delete are the major operations performed on database table for managing data.
Step-1:
Create following database and table in the MySQL.
CREATE database usersdb;
CREATE TABLE `users` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(100) NOT NULL,
`age` int(3) NOT NULL,
`email` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
);
Step-2:
Create one new folder with name 'CRUDApp' in the server and save the
following files and run the index.php
config.php
<?php $dbHost = 'localhost'; $dbName = 'usersdb'; $dbUsername = 'root'; $dbPassword = ''; $mysqli = mysqli_connect($dbHost, $dbUsername, $dbPassword, $dbName ); ?>
Step-3:
Create All the file given below in folder and run the index.php
edit.php <?php // including the database connection file include_once("config.php"); if(isset($_POST['update'])) { $id = mysqli_real_escape_string($mysqli, $_POST['id']); $name = mysqli_real_escape_string($mysqli, $_POST['name']); $age = mysqli_real_escape_string($mysqli, $_POST['age']); $email = mysqli_real_escape_string($mysqli, $_POST['email']); // checking empty fields if(empty($name) || empty($age) || empty($email)) { if(empty($name)) { echo "<font color='red'>Name field is empty.</font><br/>"; } if(empty($age)) { echo "<font color='red'>Age field is empty.</font><br/>"; } if(empty($email)) { echo "<font color='red'>Email field is empty.</font><br/>"; } } else { //updating the table $result = mysqli_query($mysqli, "UPDATE users SET name='$name',age='$age',email='$email' WHERE id=$id"); //redirectig to the display page. In our case, it is index.php header("Location: index.php"); } } ?> <?php //getting id from url $id = $_GET['id']; //selecting data associated with this particular id $result = mysqli_query($mysqli, "SELECT * FROM users WHERE id=$id"); while($res = mysqli_fetch_array($result)) { $name = $res['name']; $age = $res['age']; $email = $res['email']; } ?> <html> <head> <title>Edit Data</title> </head> <body> <a href="index.php">Home</a> <br/><br/> <form name="form1" 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>Age</td> <td><input type="text" name="age" value="<?php echo $age;?>"></td> </tr> <tr> <td>Email</td> <td><input type="text" name="email" value="<?php echo $email;?>"></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> delete.php <?php //including the database connection file include("config.php"); //getting id of the data from url $id = $_GET['id']; //deleting the row from table $result = mysqli_query($mysqli, "DELETE FROM users WHERE id=$id"); //redirecting to the display page (index.php in our case) header("Location:index.php"); ?> <?php //including the database connection file include_once("config.php"); //fetching data in descending order (lastest entry first) $result = mysqli_query($mysqli, "SELECT * FROM users ORDER BY id DESC"); // using mysqli_query instead ?> index.php <html> <head> <title>Homepage</title> </head> <body> <a href="add.html">Add New Data</a><br/><br/> <table border='1'> <tr> <td>Name</td> <td>Age</td> <td>Email</td> <td>Update</td> </tr> <?php //while($res = mysql_fetch_array($result)) { // mysql_fetch_array is deprecated, we need to use mysqli_fetch_array while($res = mysqli_fetch_array($result)) { echo "<tr>"; echo "<td>".$res['name']."</td>"; echo "<td>".$res['age']."</td>"; echo "<td>".$res['email']."</td>"; echo "<td><a href=\"edit.php?id=$res[id]\">Edit</a> | <a href=\"delete.php?id=$res[id]\" onClick=\"return confirm('Are you sure you want to delete?')\">Delete</a></td>"; } ?> </table> </body> </html>
Comments
Post a Comment