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>

 

 


Related Post

Facebook Timeline Clone v2 PHP Script Facebook Timeline Clone v2 PHP Script In this code you will learn how to make a timeline like facebook. It also includes live search, wall post, de...
Online Voting System PHP Script Free Download Online Voting System PHP Script Free Download I decided to share my simple online voting system because i know you will learn and use one or two th...
Web RSS Reader PHP Script Php script extracts rss feed in the site and displays the links of that site. Features Php reader script displays the feeds of given .xml link...
Simple Drag and Drop Image Upload with MySQL PHP S... Simple Drag and Drop Image Upload with MySQL PHP Script The Simple Drag and Drop Image Upload with MySQLi was develop by using MySQLi/jQuery. This ...
Ajax Contact Form with attachments 2.0 PHP Script ... Ajax Contact Form with attachments 2.0 Ajax Contact Form with attachments 2.0 Ajax Contact Form with Attachments and no spam control (drag and drop, r...

Have any Question or Comment?

Leave a Reply

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