CRUD Operations In PHP is a computer programming acronym that stands for create, read, update, and delete, and it refers to the four functions that are required to develop a persistent storage application: create, read, update, and delete.
Also, visit the link below for more information about other languages used in CRUD.
- Crud Operations In JavaScript With Source Code
- CRUD Operation in ASP.net with Source Code
- CRUD Operation In Django With Source Code
- Crud In Codeigniter Using Ajax With Source Code
Importance of CRUD PHP
Facilitate operation security control: in the real world, some people can read but not modify the same data, such as your income, which you can read but not update or delete (at least without authorization). Some people can only read and write their own data and have no access to the data of others.
How to Create CRUD PHP MySQL Database Connection?
To begin, establish a database connection with your PHP code.
The following code functions as a link between the webpage and the database, which will store the data from the webpage.
Name the file config.php here.
<?php $servername = "localhost"; $username = "root"; $password = ""; $dbname = "mydb"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } ?>
How to Create Records?
The first operation in PHP CRUD Operations, Create, is responsible for creating tables or new records into an existing table. To do that, first, you must write the code for the webpage to create an entry in the database.
Name the file as create.php.
<?php include "config.php"; if (isset($_POST['submit'])) { $first_name = $_POST['firstname']; $last_name = $_POST['lastname']; $email = $_POST['email']; $password = $_POST['password']; $gender = $_POST['gender']; $sql = "INSERT INTO `users`(`firstname`, `lastname`, `email`, `password`, `gender`) VALUES ('$first_name','$last_name','$email','$password','$gender')"; $result = $conn->query($sql); if ($result == TRUE) { echo "New record created successfully."; }else{ echo "Error:". $sql . "<br>". $conn->error; } $conn->close(); } ?> <!DOCTYPE html> <html> <body> <h2>Signup Form</h2> <form action="" method="POST"> <fieldset> <legend>Personal information:</legend> First name:<br> <input type="text" name="firstname"> <br> Last name:<br> <input type="text" name="lastname"> <br> Email:<br> <input type="email" name="email"> <br> Password:<br> <input type="password" name="password"> <br> Gender:<br> <input type="radio" name="gender" value="Male">Male <input type="radio" name="gender" value="Female">Female <br><br> <input type="submit" name="submit" value="submit"> </fieldset> </form> </body> </html>
How to Read/View Records?
The second operation, just as the name suggests, ‘Read’ is used to display or read the data that is already available in the database.
To perform the operation, you need to create a page that displays the records from the table ‘users’.
Now, name the page as view.php.
<?php include "config.php"; $sql = "SELECT * FROM users"; $result = $conn->query($sql); ?> <!DOCTYPE html> <html> <head> <title>View Page</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css"> </head> <body> <div class="container"> <h2>users</h2> <table class="table"> <thead> <tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th>Email</th> <th>Gender</th> <th>Action</th> </tr> </thead> <tbody> <?php if ($result->num_rows > 0) { while ($row = $result->fetch_assoc()) { ?> <tr> <td><?php echo $row['id']; ?></td> <td><?php echo $row['firstname']; ?></td> <td><?php echo $row['lastname']; ?></td> <td><?php echo $row['email']; ?></td> <td><?php echo $row['gender']; ?></td> <td><a class="btn btn-info" href="update.php?id=<?php echo $row['id']; ?>">Edit</a> <a class="btn btn-danger" href="delete.php?id=<?php echo $row['id']; ?>">Delete</a></td> </tr> <?php } } ?> </tbody> </table> </div> </body> </html>
How to Update Records?
The third operation i.e, ‘update’ is used to change or modify the already existing data present in the database.
To do this, you need to create another page to update the details in the database. Here, name the page as update.php.
<?php include "config.php"; if (isset($_POST['update'])) { $firstname = $_POST['firstname']; $user_id = $_POST['user_id']; $lastname = $_POST['lastname']; $email = $_POST['email']; $password = $_POST['password']; $gender = $_POST['gender']; $sql = "UPDATE `users` SET `firstname`='$firstname',`lastname`='$lastname',`email`='$email',`password`='$password',`gender`='$gender' WHERE `id`='$user_id'"; $result = $conn->query($sql); if ($result == TRUE) { echo "Record updated successfully."; }else{ echo "Error:" . $sql . "<br>" . $conn->error; } } if (isset($_GET['id'])) { $user_id = $_GET['id']; $sql = "SELECT * FROM `users` WHERE `id`='$user_id'"; $result = $conn->query($sql); if ($result->num_rows > 0) { while ($row = $result->fetch_assoc()) { $first_name = $row['firstname']; $lastname = $row['lastname']; $email = $row['email']; $password = $row['password']; $gender = $row['gender']; $id = $row['id']; } ?> <h2>User Update Form</h2> <form action="" method="post"> <fieldset> <legend>Personal information:</legend> First name:<br> <input type="text" name="firstname" value="<?php echo $first_name; ?>"> <input type="hidden" name="user_id" value="<?php echo $id; ?>"> <br> Last name:<br> <input type="text" name="lastname" value="<?php echo $lastname; ?>"> <br> Email:<br> <input type="email" name="email" value="<?php echo $email; ?>"> <br> Password:<br> <input type="password" name="password" value="<?php echo $password; ?>"> <br> Gender:<br> <input type="radio" name="gender" value="Male" <?php if($gender == 'Male'){ echo "checked";} ?> >Male <input type="radio" name="gender" value="Female" <?php if($gender == 'Female'){ echo "checked";} ?>>Female <br><br> <input type="submit" value="Update" name="update"> </fieldset> </form> </body> </html> <?php } else{ header('Location: view.php'); } } ?>
How to Delete Records?
The last operation of CRUD is Delete and just as the name suggests, it is used to delete an existing entry or table.
To perform this operation, you must create a page that would let you choose the data entry that you want to delete from the database.
Now, name the file delete.php.
<?php include "config.php"; if (isset($_GET['id'])) { $user_id = $_GET['id']; $sql = "DELETE FROM `users` WHERE `id`='$user_id'"; $result = $conn->query($sql); if ($result == TRUE) { echo "Record deleted successfully."; }else{ echo "Error:" . $sql . "<br>" . $conn->error; } } ?>
Project Details and Technology
Project Name: | CRUD Operations In PHP With Source Code |
Abstract | CRUD in PHP is an acronym that comes from the world of computer programming and refers to the four functions that are considered necessary to implement a persistent storage application: create, read, update and delete. |
Language/s Used: | PHP Web Framework |
PHP version (Recommended): | 5.6.3 |
Database: | MySQL |
Type: | Website, Web Application |
Developer: | Source Code Hero |
Updates: | 0 |
About the project
This 2022 CRUD Operation In PHP With Twitter Bootstrap DataTables was originally an outdated version of PHP CRUD utilizing MySQL Source Code.
However, I have already converted all of the code from MySQL to MySQLi version in this project CRUD Operation Source Code.
Again, this CRUD Operations In PHP Using Oops is a huge aid for individuals who are just getting started with PHP and MySQL. The main goal of this project is to allow the user to CREATE, READ, UPDATE, and DELETE data.
Major Functionalities / Features Of The Project
- CREATE – This feature the user can add data/information from the system and store into mysql database.
- READ – This feature the user can display all the data from the mysql database.
- UPDATE – This feature the user can edit/update data from the mysql database.
- DELETE – This feature the user can delete data from mysql database.
This CRUD Operations also includes a downloadable Source Code, simply locate and click the downloadable Button below to begin downloading.
To start executing this Project In PHP With Source Code make sure that you have a sublime or any platform of PHP and MySQL installed in your computer.
Steps On How To Run The CRUD Operations In PHP With Source Code
Time needed: 5 minutes
These are the steps on how to run CRUD Operations In PHP With Source Code.
- Download Source Code
First, find the downloadable source code below and click to start downloading the source code file.
- Extract File
Next, after finished to download the file, go to file location and right click the file and click extract.
- Copy Project Folder
Next, copy the project folder and paste it to C:\xampp\htdocs.
- Open Xampp
Next, open xampp and start the apache and mysql.
- Create Database
Next, click any browser and type to the URL localhost/phpmyadmin and create database.
- Import Database
Next, click the created database and click import to the right tab and click choose file and import the sql file inside the download folder.
- Execute Project
Final, type to the URL localhost/phpcrud
Downloadable Source Code Here!
Anyway, if you want to level up your programming knowledge, especially PHP, try this new article I’ve made for you Best PHP Projects With Source Code Free Download 2021.
Summary
As a result, this CRUD Operation in PHP Source Code is a basic project for all beginning and intermediate PHP users who want to broaden their understanding of PHP web applications. Finally, the entire PHP project with open source code is an absolute project and a valuable way for users to understand and explore more about it.
I hope this CRUD Operations in PHP MySQL will help you with what you are looking for and hope that you will learn something with this project that is useful for your future projects.
Inquiries
If you have any questions or suggestions about CRUD Operations In PHP With Source Code, please feel free to leave a comment below.
Fatal error: Uncaught mysqli_sql_exception: Incorrect integer value: ‘Null’ for column ‘people_id’ at row 1 in C:\xampp\htdocs\phpcrud\transac.php:67 Stack trace: #0 C:\xampp\htdocs\phpcrud\transac.php(67): mysqli_query(Object(mysqli), ‘INSERT INTO peo…’) #1 {main} thrown in C:\xampp\htdocs\phpcrud\transac.php on line 67
when i am going to insert data this error is appear…..
tryrty
some errors
What is the error?
hii
The code was uneditable what to do
how to view data by specic person using primary key?