• We just launched and are currently in beta. Join us as we build and grow the community.

Selecting Rows from MySQL Table using checkbox in PHP/MySQL

DFrag

Forum Architect
D Rep
0
0
0
Rep
0
D Vouches
0
0
0
Vouches
0
Posts
112
Likes
47
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this tutorial, I'm going to show you how to select rows from MySQL Table using checkbox in PHP/MySQLi. This tutorial does not include a good design but will give you knowledge on the said topic.

Creating our Database

First, we're going to create a database that contains our data.
1. Open phpMyAdmin.
2. Click databases, create a database and name it as "checkbox".
3. After creating a database, click the SQL and paste the below code. See image below for detailed instruction.

  1. CREATE

    TABLE

    `user`

    (
  2. `userid`

    INT

    (

    11

    )

    NOT

    NULL

    AUTO_INCREMENT

    ,
  3. `username`

    VARCHAR

    (

    30

    )

    NOT

    NULL

    ,
  4. `password`

    VARCHAR

    (

    30

    )

    NOT

    NULL

    ,
  5. `fullname`

    VARCHAR

    (

    60

    )

    NOT

    NULL

    ,
  6. PRIMARY

    KEY

    (

    `userid`

    )
  7. )

    ENGINE=

    InnoDB DEFAULT

    CHARSET=

    latin1;

database_5.png

Inserting Data Into our Database

Next Step in to insert some data into our database. This will serve as our list of options in our form later on.
1. Click the database "checkbox" that we have created earlier.
2. Click SQL and paste the code below.

  1. INSERT

    INTO

    `user`

    (

    `username`

    ,

    `password`

    ,

    `fullname`

    )

    VALUES
  2. (

    'neovic'

    ,

    'devierte'

    ,

    'neovic devierte'

    )

    ,
  3. (

    'julyn'

    ,

    'jolina'

    ,

    'julyn divinagracia'

    )

    ,
  4. (

    'tintin'

    ,

    'cristine'

    ,

    'cristine demapanag'

    )

    ,
  5. (

    'lee'

    ,

    'ann'

    ,

    'lee ann'

    )

    ,
  6. (

    'dee'

    ,

    'dee'

    ,

    'dee'

    )

    ;

Creating our Connection

Next step is to create a database connection and save it as "conn.php". This file will serve as our bridge between our form and our database. To create the file, open your HTML code editor and paste the code below after the tag.

  1. <?php
  2. $conn

    =

    mysqli_connect

    (

    "localhost"

    ,

    "root"

    ,

    ""

    ,

    "checkbox"

    )

    ;

  3. // Check connection
  4. if

    (

    mysqli_connect_errno

    (

    )

    )
  5. {
  6. echo

    "Failed to connect to MySQL: "

    .

    mysqli_connect_error

    (

    )

    ;
  7. }
  8. ?>

Creating our Form and Table

Lastly, we create our submit form and our options table. To create the form, open your HTML code editor and paste the code below after the tag. We name this as "index.php".

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Selecting Rows from MySQL Table using checkbox PHP, MySQLi</title>
  5. </head>
  6. <body>
  7. <h2>Select User:</h2>
  8. <div>
  9. <form method="POST">
  10. <table border="1">
  11. <thead>
  12. <th></th>
  13. <th>Username</th>
  14. <th>Password</th>
  15. <th>Full Name</th>
  16. </thead>
  17. <tbody>

  18. <?php
  19. include

    (

    'conn.php'

    )

    ;
  20. $query

    =

    mysqli_query

    (

    $conn

    ,

    "select * from `user`"

    )

    ;
  21. while

    (

    $row

    =

    mysqli_fetch_array

    (

    $query

    )

    )

    {
  22. ?>
  23. <tr>
  24. <td><input type="checkbox" value="<?php

    echo

    $row

    [

    'userid'

    ]

    ;

    ?>

    " name="id[]"></td>
  25. <td><?php

    echo

    $row

    [

    'username'

    ]

    ;

    ?>

    </td>
  26. <td><?php

    echo

    $row

    [

    'password'

    ]

    ;

    ?>

    </td>
  27. <td><?php

    echo

    $row

    [

    'fullname'

    ]

    ;

    ?>

    </td>
  28. </tr>
  29. <?php
  30. }
  31. ?>

  32. </tbody>
  33. </table>
  34. <br>
  35. <input type="submit" name="submit" value="Submit">
  36. </form>
  37. </div>
  38. <div>
  39. <h2>You Selected:</h2>
  40. <?php
  41. if

    (

    isset

    (

    $_POST

    [

    'submit'

    ]

    )

    )

    {
  42. foreach

    (

    $_POST

    [

    'id'

    ]

    as

    $id

    )

    :

  43. $sq

    =

    mysqli_query

    (

    $conn

    ,

    "select * from `user` where userid='$id

    '"

    )

    ;
  44. $srow

    =

    mysqli_fetch_array

    (

    $sq

    )

    ;
  45. echo

    $srow

    [

    'fullname'

    ]

    .

    "<br>"

    ;

  46. endforeach

    ;
  47. }
  48. ?>
  49. </div>
  50. </body>
  51. </html>

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.

2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

442,401

317,942

317,951

Top