How to Delete Data in the Database Using PHP/MySQLi

0
115

Today, you will learn How to Delete Data in the Database Using PHP/MySQLi. This method will show you on how to Delete Data into Database with the use of delete query statement in PHP and MySQLi.

 

Below are following step by step guide on how to Delete Data in the Database Using PHP and MySQLi.

 

Lets Begin:

 

First Step: Create a MySQL Database and name it “userdb“.

Second: Do the following code for creating a table in the database that you have created.

  1. CREATE TABLE IF NOT EXISTS `tbluser` (
  2. `UserID` int(11) NOT NULL AUTO_INCREMENT,
  3. `Name` varchar(30) NOT NULL,
  4. `Username` varchar(30) NOT NULL,
  5. `Pass` varchar(90) NOT NULL,
  6. PRIMARY KEY (`UserID`)

Third Step:Do the Insert Query to insert the data in the table that you have created.

  1. INSERT INTO `tbluser` (`UserID`, `Name`, `Username`, `Pass`) VALUES
  2. (1, 'Janno Palacios', 'janobe', 'admin'),
  3. (2, 'Joken villanueva', 'joken', 'joken'),
  4. (3, 'kejie palacios', 'kenjie', 'kenjie');

Fourth Step: Create a landing page and name it “index.php“.

Fifth Step: Create a CSS layout of the table.

  1. .table{
  2. width: 100%;
  3. border: solid 1px #ddd;
  4. }
  5. .table tr,
  6. .table td {
  7. position: inherit;
  8. border: solid 1px #ddd;
  9. }

Sixth Step: Do the following code for retrieving data in the database.

  1. <div>&lt;table class="table"&gt;
  2. &lt;tr&gt;
  3. &lt;td&gt;User ID&lt;/td&gt;
  4. &lt;td&gt;Name&lt;/td&gt;
  5. &lt;td&gt;Username&lt;/td&gt;
  6. &lt;td&gt;Action&lt;/td&gt;
  7. &lt;/tr&gt;&lt;?php$server="localhost";
  8. $userid ="root";
  9. $Password = "";
  10. $myDB = "userdb";
  11.  
  12. $con = mysqli_connect($server,$userid,$Password,$myDB);
  13.  
  14. echo "Failed to connect to MySQL: " . mysqli_connect_error();
  15. }
  16. $sqli="SELECT * FROM tbluser";
  17. $result=mysqli_query($con,$sqli) or die("query error");
  18. while($row=mysqli_fetch_array($result)) {
  19. echo '&lt;tr&gt;';
  20. echo '&lt;td&gt;'.$row['UserID'].'&lt;/td&gt;';
  21. echo '&lt;td&gt;'.$row['Name'].'&lt;/td&gt;';
  22. echo '&lt;td&gt;'.$row['Username'].'&lt;/td&gt;';
  23. echo '&lt;td&gt;&lt;a href="index.php?id='.$row['UserID'].'"&gt;Delete&lt;/a&gt;&lt;/td&gt;';
  24. echo '&lt;/tr&gt;';
  25. }
  26. }
  27. ?&gt;
  28. &lt;/table&gt;
  29.  
  30. </div>

Seventh Step: Do the following code for deleting data in the database.

  1. if(isset($_GET['id'])){
  2.  
  3. $id = $_GET['id'];
  4.  
  5. $sqli = "DELETE FROM `tbluser` WHERE `UserID`='{$id}'";
  6. $res = mysqli_query($con,$sqli);
  7.  
  8. if ($res) {
  9. # code...
  10. header('Location: index.php');
  11. }
  12.  
  13. }

If you have any questions or suggestion about How to Delete Data in the Database Using PHP/MySQLi, please feel free to contact us at our contact page.You can subscribe this site to see more of my tutorials.

Facebook Comments
(Visited 120 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here