Add, Edit, Delete with data table using PDO in PHP/MySQL

This simple project is created using PDO or it's called PHP Data Objects and it's a database driven using MySQL as a database. This project is intended for beginners in using PDO. It has a basic code so everyone can easily to understand and learn.

Creating our Table

We are going to make our database.
  1. Open the PHPMyAdmin.
  2. Create a database and name it as "add_pdo".
  3. After creating a database name, then we are going to create our table. And name it as "student".
  4. Kindly copy the code below.
  1. CREATE TABLE IF NOT EXISTS `student` (
  2. `student_id` INT(100) NOT NULL AUTO_INCREMENT,
  3. `fname` VARCHAR(100) NOT NULL,
  4. `mname` VARCHAR(100) NOT NULL,
  5. `lname` VARCHAR(100) NOT NULL,
  6. `address` VARCHAR(100) NOT NULL,
  7. `email` VARCHAR(100) NOT NULL,
  8. PRIMARY KEY (`student_id`)
  9. ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

Database Connection

  1. <?php $conn = new PDO('mysql:host=localhost; dbname=add_pdo','root', ''); ?>

Add, Update, and Delete in PDO Query

Modal Add Form Result
  1. <form method="post" action="add.php" enctype="multipart/form-data">
  2. <table class="table1">
  3. <tr>
  4. <td><label style="color:#3a87ad; font-size:18px;">FirstName</label></td>
  5. <td width="30"></td>
  6. <td><input type="text" name="fname" placeholder="FirstName" required /></td>
  7. </tr>
  8. <tr>
  9. <td><label style="color:#3a87ad; font-size:18px;">MiddleName</label></td>
  10. <td width="30"></td>
  11. <td><input type="text" name="mname" placeholder="MiddleName" required /></td>
  12. </tr>
  13. <tr>
  14. <td><label style="color:#3a87ad; font-size:18px;">LastName</label></td>
  15. <td width="30"></td>
  16. <td><input type="text" name="lname" placeholder="LastName" required /></td>
  17. </tr>
  18. <tr>
  19. <td><label style="color:#3a87ad; font-size:18px;">Address</label></td>
  20. <td width="30"></td>
  21. <td><input type="text" name="address" placeholder="Address" required /></td>
  22. </tr>
  23. <tr>
  24. <td><label style="color:#3a87ad; font-size:18px;">Email</label></td>
  25. <td width="30"></td>
  26. <td><input type="email" name="email" placeholder="Email" required /></td>
  27. </tr>
  28.  
  29. </table>
  30.  
  31.  
  32. </div>
  33. <div class="modal-footer">
  34. <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
  35. <button type="submit" name="Submit" class="btn btn-primary">Add</button>
  36. </div>
  37.  
  38.  
  39. </form>
Add PDO Query
  1. <?php
  2. require_once('db.php');
  3.  
  4. $fname= $_POST['fname'];
  5. $mname= $_POST['mname'];
  6. $lname= $_POST['lname'];
  7. $address= $_POST['address'];
  8. $email= $_POST['email'];
  9.  
  10. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  11. $sql = "INSERT INTO student (fname, mname, lname, address, email)
  12. VALUES ('$fname', '$mname', '$lname', '$address', '$email')";
  13.  
  14. $conn->exec($sql);
  15. echo "<script>alert('Account successfully added!'); window.location='index.php'</script>";
  16. ?>
Result After Adding Data ResultEdit Form Result
  1. <?php
  2. include('db.php');
  3. $result = $conn->prepare("SELECT * FROM student where student_id='$ID'");
  4. $result->execute();
  5. for($i=0; $row = $result->fetch(); $i++){
  6. $id=$row['student_id'];
  7. ?>
  8. <form class="form-horizontal" method="post" action="edit_PDO.php<?php echo '?student_id='.$id; ?>" enctype="multipart/form-data" style="float: right;">
  9. <legend><h4>Edit</h4></legend>
  10. <h4>Personal Information</h4>
  11. <hr>
  12. <div class="control-group">
  13. <label class="control-label" for="inputPassword">FirstName:</label>
  14. <div class="controls">
  15. <input type="text" name="fname" required value=<?php echo $row['fname']; ?>>
  16. </div>
  17. </div>
  18. <div class="control-group">
  19. <label class="control-label" for="inputPassword">MiddleName:</label>
  20. <div class="controls">
  21. <input type="text" name="mname" required value=<?php echo $row['mname']; ?>>
  22. </div>
  23. </div>
  24. <div class="control-group">
  25. <label class="control-label" for="inputEmail">LastName:</label>
  26. <div class="controls">
  27. <input type="text" name="lname" required value=<?php echo $row['lname']; ?>>
  28. </div>
  29. </div>
  30. <div class="control-group">
  31. <label class="control-label" for="inputPassword">Address:</label>
  32. <div class="controls">
  33. <input type="text" name="address" required value=<?php echo $row['address']; ?>>
  34. </div>
  35. </div>
  36. <div class="control-group">
  37. <label class="control-label" for="inputPassword">Email:</label>
  38. <div class="controls">
  39. <input type="email" name="email" required value=<?php echo $row['email']; ?>>
  40. </div>
  41. </div>
  42.  
  43. <div class="control-group">
  44. <div class="controls">
  45.  
  46. <button type="submit" name="update" class="btn btn-success" style="margin-right: 65px;">Save</button>
  47. <a href="index.php" class="btn">Back</a>
  48. </div>
  49. </div>
  50. </form>
  51. <?php } ?>
Edit PDO Query
  1. <?php
  2. include 'db.php';
  3.  
  4. $get_id=$_REQUEST['student_id'];
  5.  
  6. $fname= $_POST['fname'];
  7. $mname= $_POST['mname'];
  8. $lname= $_POST['lname'];
  9. $address= $_POST['address'];
  10. $email= $_POST['email'];
  11.  
  12. $sql = "UPDATE student SET fname ='$fname', mname ='$mname', lname ='$lname',
  13. address ='$address', email ='$email' WHERE student_id = '$get_id' ";
  14.  
  15. $conn->exec($sql);
  16. echo "<script>alert('Successfully Edit The Account!'); window.location='index.php'</script>";
  17. ?>
Result After Edditing Data ResultModal Delete Form Result
  1. <div id="delete<?php echo $id;?>" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  2. <div class="modal-header">
  3. <h3 id="myModalLabel">Delete</h3>
  4. </div>
  5. <div class="modal-body">
  6. <p><div style="font-size:larger;" class="alert alert-danger">Are you Sure you want Delete <b style="color:red;"><?php echo $row['fname']." ".$row['mname']." ".$row['lname'] ; ?></b> Data?</p>
  7. </div>
  8. <hr>
  9. <div class="modal-footer">
  10. <button class="btn btn-inverse" data-dismiss="modal" aria-hidden="true">No</button>
  11. <a href="delete.php<?php echo '?student_id='.$id; ?>" class="btn btn-danger">Yes</a>
  12. </div>
  13. </div>
  14. </div>
Delete PDO Query
  1. <?php
  2. require_once('db.php');
  3.  
  4. $get_id=$_GET['student_id'];
  5.  
  6. // sql to delete a record
  7. $sql = "Delete from student where student_id = '$get_id'";
  8.  
  9. // use exec() because no results are returned
  10. $conn->exec($sql);
  11. header('location:index.php');
  12. ?>
Result After Deleting Data Result And, that's all, you can have Add, Edit, Delete with data table using PDO in PHP/MySQL or kindly click the "Download Code" button to download the full source code. This is the full source code for Inserting Data, Updating Data, and Deleting Data in MySQL. Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.

Comments

Submitted byEu (not verified)on Thu, 01/19/2017 - 14:30

Why is modal add form is incomplete?
Submitted byDennis L. (not verified)on Thu, 02/03/2022 - 02:59

I have tried this so many times I cannot get this to work for me, I keep getting close, I have had to modify the DB.php to another one I found online cause this one didn't work at all for me, I have finally gotten to the point where the main page finds the records I have in the database, but it will not display them, the table says there are 14 entries, and there are but it will not display them. I am not a coder, but I have changed them.. https://bt.inokamanori.com/dataupdate1/index.php. I am at a loss. the error I am getting now is. [02-Feb-2022 18:51:47 UTC] PHP Warning: Trying to access array offset on value of type bool in /home/g88pa0itn9u7/bt.inokamanori.com/dataupdate1/index.php on line 37 [02-Feb-2022 18:51:47 UTC] PHP Warning: Trying to access array offset on value of type bool in /home/g88pa0itn9u7/bt.inokamanori.com/dataupdate1/index.php on line 40 [02-Feb-2022 18:51:47 UTC] PHP Warning: Trying to access array offset on value of type bool in /home/g88pa0itn9u7/bt.inokamanori.com/dataupdate1/index.php on line 41 [02-Feb-2022 18:51:47 UTC] PHP Warning: Trying to access array offset on value of type bool in /home/g88pa0itn9u7/bt.inokamanori.com/dataupdate1/index.php on line 42 [02-Feb-2022 18:51:47 UTC] PHP Warning: Trying to access array offset on value of type bool in /home/g88pa0itn9u7/bt.inokamanori.com/dataupdate1/index.php on line 43 [02-Feb-2022 18:51:47 UTC] PHP Warning: Trying to access array offset on value of type bool in /home/g88pa0itn9u7/bt.inokamanori.com/dataupdate1/index.php on line 44 [02-Feb-2022 18:51:47 UTC] PHP Warning: Trying to access array offset on value of type bool in /home/g88pa0itn9u7/bt.inokamanori.com/dataupdate1/index.php on line 56 [02-Feb-2022 18:51:47 UTC] PHP Warning: Trying to access array offset on value of type bool in /home/g88pa0itn9u7/bt.inokamanori.com/dataupdate1/index.php on line 56 [02-Feb-2022 18:51:47 UTC] PHP Warning: Trying to access array offset on value of type bool in /home/g88pa0itn9u7/bt.inokamanori.com/dataupdate1/index.php on line 56

Add new comment