PHP CRUD Scripts

PHP Simple CRUD Application Script

PHP Simple CRUD Application Script Zip:

What is CRUD?

CRUD refers to the four basic types of Database operations: Create, Read, Update, Delete. Most applications and projects perform some kind of CRUD functionality.

Once you learn about these CRUD operations, you can use them for many projects. For an example, if you learn how to create student table with multiple columns, you can use similar approach to create employee table or customers table.


About Simple CRUD Script:

We have created a Simple CRUD (Create/Read/Update/Delete) Application in PHP that is easy to understand. You can directly download all the code/database using the Download button.

This CRUD Script has the minimum number of functions required, so that anybody can easily understand the code. This script does not ensure security and data validation. So, if you want to use this script for your live project, please make sure to validation user data.


How to run CRUD Script:

You just need to run the given code files and database at your server. This script should run properly after database setup on most local server. But it may require few configuration changes on remote/live server.

  • Download CRUD Script Zip file and extract it.
  • Copy complete crud folder into your local server (in htdocs or www)
  • Create a new database "crud_db” (You can use PHPMyAdmin)
  • Import database.sql file from crud folder into database "crud_db
  • Now run files from crud folder using your server/folder path (For example: http://localhost/crud/)
  • It should works properly. You can Add some user data. Then you can test Edit/Update/Delete-User functions as well

You can see code from all the files in this tutorial. You can find basic description in comments as well.


config.php
<?php
/**
 * using mysqli_connect for database connection
 */
 
$databaseHost = 'localhost';
$databaseName = 'crud_db';
$databaseUsername = 'root';
$databasePassword = '';
 
$mysqli = mysqli_connect($databaseHost, $databaseUsername, $databasePassword, $databaseName); 
 
?>

config.php file store information about the database host, username and password. Most of the local server works with given detail. You can change as per your host and database details.


database.sql
/* Create Database and Table */
create database crud_db;
 
use crud_db;
 
CREATE TABLE `users` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(100),
  `email` varchar(100),
  `mobile` varchar(15),
  PRIMARY KEY  (`id`)
);

database.sql is a database file. You can to create a database, after downloading the crud script. You can either run commands from above mysql file or simply import this ‘database.sql’ file into database (example using PHPMyAdmin).


index.php
<?php
// Create database connection using config file
include_once("config.php");
 
// Fetch all users data from database
$result = mysqli_query($mysqli, "SELECT * FROM users ORDER BY id DESC");
?>
 
<html>
<head>    
    <title>Homepage</title>
</head>
 
<body>
<a href="add.php">Add New User</a><br/><br/>
 
    <table width='80%' border=1>
 
    <tr>
        <th>Name</th> <th>Mobile</th> <th>Email</th> <th>Update</th>
    </tr>
    <?php  
    while($user_data = mysqli_fetch_array($result)) {         
        echo "<tr>";
        echo "<td>".$user_data['name']."</td>";
        echo "<td>".$user_data['mobile']."</td>";
        echo "<td>".$user_data['email']."</td>";    
        echo "<td><a href='edit.php?id=$user_data[id]'>Edit</a> | <a href='delete.php?id=$user_data[id]'>Delete</a></td></tr>";        
    }
    ?>
    </table>
</body>
</html>

index.php file is the main file which include configuration file for database connection. Then it display all users list using MySQL Select Query. However, you need to add some users first using ‘Add New User’ link.


add.php
<html>
<head>
	<title>Add Users</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>Name</td>
				<td><input type="text" name="name"></td>
			</tr>
			<tr> 
				<td>Email</td>
				<td><input type="text" name="email"></td>
			</tr>
			<tr> 
				<td>Mobile</td>
				<td><input type="text" name="mobile"></td>
			</tr>
			<tr> 
				<td></td>
				<td><input type="submit" name="Submit" value="Add"></td>
			</tr>
		</table>
	</form>
	
	<?php
 
	// Check If form submitted, insert form data into users table.
	if(isset($_POST['Submit'])) {
		$name = $_POST['name'];
		$email = $_POST['email'];
		$mobile = $_POST['mobile'];
		
		// include database connection file
		include_once("config.php");
				
		// Insert user data into table
		$result = mysqli_query($mysqli, "INSERT INTO users(name,email,mobile) VALUES('$name','$email','$mobile')");
		
		// Show message when user added
		echo "User added successfully. <a href='index.php'>View Users</a>";
	}
	?>
</body>
</html>

add.php file is responsible to add new users. HTML Form is used to capture user data. After User data is submitted, MySQL INSERT Query is used to insert user data into database. You can “View User” after user added.


edit.php
<?php
// include database connection file
include_once("config.php");
 
// Check if form is submitted for user update, then redirect to homepage after update
if(isset($_POST['update']))
{	
	$id = $_POST['id'];
	
	$name=$_POST['name'];
	$mobile=$_POST['mobile'];
	$email=$_POST['email'];
		
	// update user data
	$result = mysqli_query($mysqli, "UPDATE users SET name='$name',email='$email',mobile='$mobile' WHERE id=$id");
	
	// Redirect to homepage to display updated user in list
	header("Location: index.php");
}
?>
<?php
// Display selected user data based on id
// Getting id from url
$id = $_GET['id'];
 
// Fetech user data based on id
$result = mysqli_query($mysqli, "SELECT * FROM users WHERE id=$id");
 
while($user_data = mysqli_fetch_array($result))
{
	$name = $user_data['name'];
	$email = $user_data['email'];
	$mobile = $user_data['mobile'];
}
?>
<html>
<head>	
	<title>Edit User Data</title>
</head>
 
<body>
	<a href="index.php">Home</a>
	<br/><br/>
	
	<form name="update_user" 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="email" value=<?php echo $email;?>></td>
			</tr>
			<tr> 
				<td>Mobile</td>
				<td><input type="text" name="mobile" value=<?php echo $mobile;?>></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>

edit.php is used to edit/update user data once user click on’Edit’ link. It first fetch user’s current data in form. You can change user data and update it. It will redirect to homepage, after successful update.


delete.php
<?php
// include database connection file
include_once("config.php");
 
// Get id from URL to delete that user
$id = $_GET['id'];
 
// Delete user row from table based on given id
$result = mysqli_query($mysqli, "DELETE FROM users WHERE id=$id");
 
// After delete redirect to Home, so that latest user list will be displayed.
header("Location:index.php");
?>

delete.php file simply called when we click on ‘Delete’ link for any user. It will delete selected user. Delete/edit/update operations user particular user_id to identify users.


Common Mistakes while running CRUD Script

  • Make sure you properly downloaded and extracted all files
  • Make sure you have created database properly with the same as as mentioned in config.php
  • When running CRUD script, make sure folder name is right. Sometimes there are folder inside folders. Adjust your path or folder accordingly.
  • For any other problem, you can send your error screenshot to tutorialsclass.online@gmail.com or contact us at facebook page

We will update next version of the CRUD script with basic validation functions. See you back.


PHP Simple CRUD Application Script Zip Download: