Database
Create a new MySQL database named mydemo and execute the SQL code below:
-- phpMyAdmin SQL Dump
-- version 4.1.14
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Oct 30, 2015 at 10:05 PM
-- Server version: 5.6.17
-- PHP Version: 5.5.12
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: `mydemo`
--
-- --------------------------------------------------------
--
-- Table structure for table `account`
--
CREATE TABLE IF NOT EXISTS `account` (
`username` varchar(250) NOT NULL,
`password` varchar(250) NOT NULL,
`age` int(11) NOT NULL,
`email` varchar(250) NOT NULL,
`website` varchar(250) NOT NULL,
PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `account`
--
INSERT INTO `account` (`username`, `password`, `age`, `email`, `website`) VALUES
('acc1', '123', 20, 'acc1@gmail.com', 'http://google.com'),
('acc2', '123', 21, 'acc2@gmail.com', 'http://google.com'),
('acc4', '123', 22, 'acc4@gmail.com', 'http://google.com'),
('acc5', '123', 22, 'acc4@gmail.com', 'http://google.com'),
('acc6', '123', 22, 'acc4@gmail.com', 'http://google.com');
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
Database Connection
Create PHP file named connect.php. Use PDO connect to demo database with default account:
Username: root
Password:
<?php
$conn = new PDO("mysql:host=localhost;dbname=mydemo", 'root', '');
?>
List All Account
Create PHP file named index.php. This file will list all accounts from the account table and allows users to select multiple accounts to delete
<script type="text/javascript" src="js/jquery-1.6.2.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#checkBoxAll').click(function() {
if ($(this).is(":checked"))
$('.chkCheckBoxId').prop('checked', true);
else
$('.chkCheckBoxId').prop('checked', false);
});
});
</script>
<?php
require 'database.php';
if(isset($_POST['buttonDelete'])) {
if(isset($_POST['username'])) {
foreach ($_POST['username'] as $username) {
$stmt = $conn->prepare('delete from account where username = :username');
$stmt->bindValue('username', $username);
$stmt->execute();
}
}
}
$stmt = $conn->prepare('select * from account');
$stmt->execute();
?>
<form method="post" action="index.php">
<input type="submit" name="buttonDelete" value="Delete" onclick="return confirm('Are you sure?')" />
<table cellpadding="2" cellspacing="2" border="1">
<tr>
<th><input type="checkbox" id="checkBoxAll" /></th>
<th>Username</th>
<th>Email</th>
<th>Age</th>
</tr>
<?php while($account = $stmt->fetch(PDO::FETCH_OBJ)) { ?>
<tr>
<td><input type="checkbox" class="chkCheckBoxId"
value="<?php echo $account->username; ?>" name="username[]" /></td>
<td><?php echo $account->username; ?></td>
<td><?php echo $account->email; ?></td>
<td><?php echo $account->age; ?></td>
</tr>
<?php } ?>
</table>
</form>
Demo
Accounts List Page
Select the accounts to delete
Accounts List Page After Delete
References
I recommend you refer to the books below to learn more about the knowledge in this article:
- Murach’s PHP and MySQL (3rd Edition)
- Learning PHP, MySQL & JavaScript: With jQuery, CSS & HTML5 (Learning Php, Mysql, Javascript, Css & Html5)
- PHP and MySQL Web Development (5th Edition) (Developer’s Library)
- Murach’s MySQL, 2nd Edition
- MySQL (5th Edition) (Developer’s Library)
- PHP Ajax Cookbook