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.
CREATE TABLE `user` (
`userid` INT(11) NOT NULL AUTO_INCREMENT,
`username` VARCHAR(30) NOT NULL,
`password` VARCHAR(30) NOT NULL,
`fullname` VARCHAR(60) NOT NULL,
PRIMARY KEY (`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
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.
INSERT INTO `user` (`username`, `password`, `fullname`) VALUES
('neovic', 'devierte', 'neovic devierte'),
('julyn', 'jolina', 'julyn divinagracia'),
('tintin', 'cristine', 'cristine demapanag'),
('lee', 'ann', 'lee ann'),
('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.
<?php
// Check connection
{
}
?>
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".
<!DOCTYPE html>
<html>
<head>
<title>Selecting Rows from MySQL Table using checkbox PHP, MySQLi</title>
</head>
<body>
<h2>Select User:</h2>
<div>
<form method="POST">
<table border="1">
<thead>
<th></th>
<th>Username</th>
<th>Password</th>
<th>Full Name</th>
</thead>
<tbody>
<?php
include('conn.php');
?>
<tr>
<td><input type="checkbox" value="<?php echo $row['userid']; ?>" name="id[]"></td>
<td><?php echo $row['username']; ?></td>
<td><?php echo $row['password']; ?></td>
<td><?php echo $row['fullname']; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
<br>
<input type="submit" name="submit" value="Submit">
</form>
</div>
<div>
<h2>You Selected:</h2>
<?php
if (isset($_POST['submit'])){
foreach ($_POST['id'] as $id):
$sq=mysqli_query($conn,"select * from `user` where userid='$id'");
echo $srow['fullname']. "<br>";
endforeach;
}
?>
</div>
</body>
</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.