Google Ads

Selecting Rows from MySQL Table using checkbox in PHP/MySQL PHP Script


Selecting Rows from MySQL Table using checkbox in PHP/MySQL PHP Script

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
$conn = mysqli_connect("localhost","root","","checkbox");

// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>

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');
$query=mysqli_query($conn,"select * from `user`");
while($row=mysqli_fetch_array($query)){
?>
<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'");
$srow=mysqli_fetch_array($sq);
echo $srow['fullname']. "<br>";

endforeach;
}
?>
</div>
</body>
</html>

 

 

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *