Create,Read,Update And Delete With PHP Mysqli-CRUD.

All operations you will carry out with php mysqli will revolve around this 4 functions of creating or inserting data to the database,reading the data from the database,updating or editing the data and deleting data from the db.

So if you want to have a clear understanding of how it works follow the steps below:

1.Create a database on your phpmyadmin dashboard and create a table to store users data with the following structure.

2.Create a main folder in your xampp htdocs or www in wamp,you can call it phpcrud or any name you may want.

3. Create another folder in your main folder(phpcrud) and call it config.

4.Open your text editor and create a file named connection.php and paste the following code:

NOTE:This file is used for database connection.

5.On your file editor create another file named create.php and save it in your main folder(phpcrud).

<?php ="Fried">Fried Eggs </option></select><br/><input type="submit" name="submit" value="Submit"/></form><?php if(!empty($_GET['error'])){$err = $_GET['error'];switch($err){case 0:echo"<p>cessfully";break;case 1:echo"<p>Failled.</p>";break;     } }?></center></html>

Open your browser and type this url localhost/phpcrud/create.php to submit some data into the database.Upto this point you should be able to insert some data into your db.

6.Create another file in your text editor and call it read.php and paste the following code:

<?phpinclude_once('config/connection.php');if(!empty($_GET['act'])){$uid = $_GET['id'];$sql = "DELETE FROM users WHERE id = '$uid'";mysqli_query($con, $sql);}?><!DOCTYPE html><html><head><title>Phpcrud</title></head><body><center><h3>Fetching data from database and Deleting.</h3><table><tr><td>Id</td><td>Username</td><td>Fullname</td><td>Email</td><td>Meal</td><td>Action</td></tr><?php$sql = "select * from users ";$qry = mysqli_query($con, $sql);while ($row = mysqli_fetch_array($qry)){$id = $row['id'];$username = $row['username'];$fullname = $row['fullname'];$email = $row['email'];$meal = $row['meal'];?><tr align="center"><td><?php echo $id; ?></td><td><?php echo $username; ?></td><td><?php echo $fullname; ?></td><td><?php echo $email; ?></td><td><?php echo $meal; ?></td><td><a href="update.php?id=<?php echo $id; ?>"> Edit</a> <a href="read.php?id=<?php echo $id; ?>&act=del">Delete</a></td></tr><?php } ?></table></center></body></html>

The above code should be able to fetch data from db,delete on clicking on Delete and edit on clicking on Edit.

7.Create another file called update.php for editng of records and paste the following code.

<?phpinclude_once('config/connection.php');$username = "";$fullname = "";$email = "";$meal = "";$uid = 0;if(!empty($_GET['id'])){$uid = $_GET['id'];$sql = "SELECT * FROM users WHERE id = '$uid' LIMIT 1";$res = mysqli_query($con,$sql);if ($res) {$rows = mysqli_fetch_assoc($res);$username = $rows['username'];$fullname = $rows['fullname'];$email = $rows['email'];$meal = $rows['meal'];}}if(isset($_POST['submit'])){$username = $_POST['username'];$fullname = $_POST['fullname'];$email = $_POST['email'];$meal = $_POST['meal']; $sql = "UPDATE users SET username = '$username',fullname = '$fullname',email = '$email',meal = '$meal' WHERE id = '$uid'";if(mysqli_query($con,$sql)){header('Location:read.php');}else{header('Location:read.php'.mysqli_error($con));}}?><!DOCTYPE html><html><head><title>Phpcrud</title></head><body><center><h3>Updating Data From Database.</h3><form action="" method="post"><input type="text" placeholder="Enter your username" value="<?php echo $username; ?>" required="required" name="username"/><br/><input type="text" placeholder="Enter your fullname" value="<?php echo $fullname; ?>" required="required" name="fullname"/><br/><input type="email" name="email" placeholder="Enter your email address" value="<?php echo $email; ?>" required="required"/><br/><select name="meal" required="required"><option value="">--Select Meal--</option><option value="Ugali Chicken">Ugali Chicken </option><option value="Rice Chicken">Rice Chicken </option><option value="Pizza">Pizza </option><option value="Githeri">Githeri </option><option value="Nyama Choma">Nyama Choma </option><option value="Fried Eggs">Fried Eggs </option></select><br/><input type="submit" name="submit" value="Submit"/></form></center></body></html>

Now we are through with php crud operations and you can customise the above code to fit your own system.Hope you understood if you have any questions please feel free to post in Comments section below.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *