PHP - Get Selected Row Data

In this tutorial we will create a Get Selected Row Data using PHP. This code will get the data base on the selected row when the user click the submit button. The code use MySQLi SELECT and adding a IN parameter in the WHERE statement to enable the accepting of an array data in the query. 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 interpret 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 And, this is the link for the bootstrap that i used for the layout design

Creating Database

Open your database web server then create a database name in it db_get, 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_get");
  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="">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Get Selected Row Data</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-2"></div>
  18. <div class="col-md-8">
  19. <form method="POST" action="">
  20. <table class="table table-bordered">
  21. <thead class="alert-info">
  22. <tr>
  23. <th>#</th>
  24. <th>Product</th>
  25. <th>Price</th>
  26. </tr>
  27. </thead>
  28. <tbody>
  29. <?php
  30. require 'conn.php';
  31. $query=mysqli_query($conn, "SELECT * FROM `product`") or die(mysqli_error());
  32. while($fetch=mysqli_fetch_array($query)){
  33. ?>
  34. <tr>
  35. <td><input type="checkbox" name="p_id[]" value="<?php echo $fetch['product_id']?>"/></td>
  36. <td><?php echo $fetch['product_name']?></td>
  37. <td><?php echo $fetch['price']?></td>
  38. </tr>
  39. <?php
  40. }
  41. ?>
  42. </tbody>
  43. </table>
  45. <center><button name="get" class="btn btn-primary">Get selected</button></center>
  46. </form>
  48. <?php include 'get_select.php'?>
  49. </div>
  50. </div>
  51. </body>
  52. </html>

Creating Main Function

This code contains the main function of the application. This code will display the select row when the button is clicked. To do that just copy and write this block of codes inside the text editor, then save it as get_select.php.
  1. <?php
  2. require 'conn.php';
  3. if(ISSET($_POST['get'])){
  4. if(!empty($_POST['p_id'])){
  5. echo "<h2>You select:</h2>";
  6. $query = mysqli_query($conn, "SELECT * FROM `product` WHERE `product_id` IN (".implode(',',$_POST['p_id']).")") or die(mysqli_error());
  7. echo "<table class='table table-bordered'>";
  8. while($fetch=mysqli_fetch_array($query)){
  9. echo "<tr>
  10. <td>".$fetch['product_name']."</td>
  11. <td>".$fetch['price']."</td>
  12. </tr>";
  13. }
  14. echo "</table>";
  15. }
  16. }
  17. ?>
There you have it we successfully created Get Selected Row Data 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