PHP - Remove List With MySQLi

Submitted by: 
Language: 
Visitors have accessed this post 112 times.

In this tutorial we will create a Remove List With MySQLi using PHP. This code will remove a MySQLi data in the HTML list when user click the icon button. The code use href or Hypertext Referencing to direct the user to go to other page that has been binded by an id, in order to remove a list data using DELETE query and adding the binded id in theWHERE clause. This a user-friendly program feel free to modify and use it to your system.

We will be using PHP as a scripting language that interepret in the webserver such as xamp, wamp, etc. It is widely use by modern website application to handle and protect user confidential information.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html.

And this is the link for the jquery that i used in this tutorial https://jquery.com/.

Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_list, after that click Import then locate the database file inside the folder of the application then click ok.
tut1

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.

  1. <?php
  2. $conn=mysqli_connect("localhost", "root", "", "db_list");
  3.  
  4. if(!$conn){
  5. die("Error: Failed to connect to database!");
  6. }
  7. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="<a href="https://sourcecodester.com">Sourcecodester</a>
  11. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  12. </a> <div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Remove List With MySQLi</h3>
  17. <hr style="border-top:1px dotted #ccc;">
  18. <div class="col-md-9">
  19. <h2>MY MOVIE LIST <?php echo date("Y")?></h2>
  20. <ul>
  21. <?php
  22. require'conn.php';
  23.  
  24. $query=mysqli_query($conn, "SELECT * FROM `movie`") or die(mysqli_error());
  25. while($fetch=mysqli_fetch_array($query)){
  26. echo "<li>".$fetch['movie_title']." <a href='remove.php?id=".$fetch['movie_id']."'><span class='glyphicon glyphicon-remove'></span></a></li>";
  27. }
  28. ?>
  29. </ul>
  30. </div>
  31. <div class="col-md-3">
  32. <form method="POST" action="add.php">
  33. <div class="form-group">
  34. <label>Enter a movie title</label>
  35. <input type="text" name="movie" class="form-control" required="required"/>
  36. </div>
  37. <center><button class="btn btn-primary" name="add">Add</button></center>
  38. </form>
  39. </div>
  40. </div>
  41. </body>
  42. </html>

Creating the PHP Query

This code contains the php query of the application. This code will store the POST inputs to the MySQLi database. To make this just copy and write these block of codes below inside the text editor, then save it as add.php

  1. <?php
  2. require_once'conn.php';
  3.  
  4. if(ISSET($_POST['add'])){
  5. $movie=$_POST['movie'];
  6.  
  7. mysqli_query($conn, "INSERT INTO `movie` VALUES('', '$movie')") or die(mysqli_error());
  8.  
  9. header('location: index.php');
  10. }
  11. ?>

Creating the Main Function

This code contains the main function of the application. This code will remove the MySQLi data in the list when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as remove.php

  1. <?php
  2. require_once'conn.php';
  3.  
  4. if(ISSET($_REQUEST['id'])){
  5. $id=$_REQUEST['id'];
  6.  
  7. mysqli_query($conn, "DELETE FROM `movie` WHERE `movie_id`='$id'") or die(mysqli_error());
  8.  
  9. header('location:index.php');
  10. }
  11. ?>

There you have it we successfully created Remove List With MySQLi using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.