<?php
// Database Connection File - 'database.php
class Database {
// Connection variables
private $host = "localhost";
private $dbName = "ccrp_db";
private $username = "root";
private $password = "";
public $conn;
// Method return security connection
public function dbConnection() {
$this->conn = null;
try {
$this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->dbName, $this->username, $this->password, array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
));
} catch (PDOException $exception) {
echo "Connection error: " . $exception->getMessage();
}
return $this->conn;
}
}
?>
<?php
// Member Class file - member.php
require_once 'database.php';
class Member {
private $conn;
// Constructor
public function __construct(){
$database = new Database();
$db = $database->dbConnection();
$this->conn = $db;
}
// Execute queries SQL
public function runQuery($sql){
$stmt = $this->conn->prepare($sql);
return $stmt;
}
// Insert
public function insert($name, $residential_address, $mailing_address, $precinct, $age, $ethnicity, $gender, $party, $race, $phone){
try{
$stmt = $this->conn->prepare("INSERT INTO members (name, residential_address, mailing_address, precinct, age, ethnicity, gender, party, race, phone) VALUES(:name, :residential_address, :mailing_address, :precinct, :age, :ethnicity, :gender, :party, :race, :phone)");
$stmt->bindparam(":name", $name);
$stmt->bindparam(":residential_address", $residential_address);
$stmt->bindparam(":mailing_address", $mailing_address);
$stmt->bindparam(":precinct", $precinct);
$stmt->bindparam(":age", $age);
$stmt->bindparam(":ethnicity", $ethnicity);
$stmt->bindparam(":gender", $gender);
$stmt->bindparam(":party", $party);
$stmt->bindparam(":race", $race);
$stmt->bindparam(":phone", $phone);
$stmt->execute();
return $stmt;
}catch(PDOException $e){
echo $e->getMessage();
}
}
// Update
public function update($name, $residential_address, $mailing_address, $precinct, $age, $ethnicity, $gender, $party, $race, $phone, $id){
try{
$stmt = $this->conn->prepare("UPDATE members SET name = :name, residential_address = :residential_address, mailing_address = :mailing_address, precinct = :precinct, age = :age, ethnicity = :ethnicity, gender = :gender, party = :party, race = :race, phone = :phone WHERE id = :id");
$stmt->bindparam(":name", $name);
$stmt->bindparam(":residential_address", $residential_address);
$stmt->bindparam(":mailing_address", $mailing_address);
$stmt->bindparam(":precinct", $precinct);
$stmt->bindparam(":age", $age);
$stmt->bindparam(":ethnicity", $ethnicity);
$stmt->bindparam(":gender", $gender);
$stmt->bindparam(":party", $party);
$stmt->bindparam(":race", $race);
$stmt->bindparam(":phone", $phone);
$stmt->bindparam(":phone", $id);
$stmt->execute();
return $stmt;
}catch(PDOException $e){
echo $e->getMessage();
}
}
// Delete
public function delete($id){
try{
$stmt = $this->conn->prepare("DELETE FROM members WHERE id = :id");
$stmt->bindparam(":phone", $id);
$stmt->execute();
return $stmt;
}catch(PDOException $e){
echo $e->getMessage();
}
}
// Redirect URL method
public function redirect($url){
header("Location: $url");
}
}
?>
<?php
// Form to Add New Members - form.php
// Show PHP errors
ini_set('display_errors',1);
ini_set('display_startup_erros',1);
error_reporting(E_ALL);
require_once 'classes/member.php';
$objMember = new Member();
// GET
if(isset($_GET['edit_id'])){
$id = $_GET['edit_id'];
$stmt = $objMember->runQuery("SELECT * FROM members WHERE id=:id");
$stmt->execute(array(":id" => $id));
$rowMember = $stmt-fetch(PDO::FETCH_ASSOC);
}else{
$id = null;
$rowMember = null;
}
// POST
if(isset($_POST['btn_save'])){
$name = strip_tags($_POST['name']);
$residential_address = strip_tags($_POST['residential_address']);
$mailing_address = strip_tags($_POST['mailing_address']);
$precinct = strip_tags($_POST['precinct']);
$age = strip_tags($_POST['age']);
$ethnicity = strip_tags($_POST['ethnicity']);
$gender = strip_tags($_POST['gender']);
$party = strip_tags($_POST['party']);
$race = strip_tags($_POST['race']);
$phone = strip_tags($_POST['phone']);
try {
if($id !=null){
if($objMember->update($name, $residential_address, $mailing_address, $precinct, $age, $ethnicity, $gender, $party, $race, $phone)){
$objMember->redirect('index.php?updated');
}
}else{
if($objMember->insert($name, $residential_address, $mailing_address, $precinct, $age, $ethnicity, $gender, $party, $race, $phone)){
$objMember->redirect('index.php?inserted');
}else{
$ocjMember->redirect('index.php?error');
}
}
}catch(PDOException $e){
echo $e->getMessage();
}
}
?>
<!doctype html>
<html lang="en">
<head>
<!-- Head metas, css, and title -->
<?php require_once 'includes/head.php'; ?>
</head>
<body>
<!-- Header banner -->
<?php require_once 'includes/header.php'; ?>
<div class="container-fluid">
<div class="row">
<!-- Sidebar menu -->
<?php require_once 'includes/sidebar.php'; ?>
<main role="main" class="col-md-9 ml-sm-auto col-lg-10 px-4">
<h1 style="margin-top:10px">Add/Edit Members</h1>
<!-- <p>Required fields are in * </p>-->
<form method="post">
<div class="form-group">
<label for="id">ID</label>
<input class="form-control" type="number" name="id" id="id" value="<?php print($rowMember['id']); ?>" readonly>
</div>
<div class="form-group">
<label for="name">Name</label>
<input class="form-control" type="text" name="name" id="name" placeholder="Last, First Middle" value="<?php print($rowMember['name']); ?>" maxlength="40">
</div>
<div class="form-group">
<label for="residential_address">Residential Address</label>
<input class="form-control" type="text" name="residential_address" id="residential_address" value="<?php print($rowMember['residential_address']); ?>" maxlength="50">
</div>
<div class="form-group">
<label for="mailing_address">Mailing Address</label>
<input class="form-control" type="text" name="mailing_address" id="mailing_address" value="<?php print($rowMember['mailing_address']); ?>" maxlength="65">
</div>
<div class="form-group">
<label for="precinct">Precinct</label>
<input class="form-control" type="text" name="precinct" id="precinct" placeholder="##-##" value="<?php print($rowMember['precinct']); ?>" maxlength="5">
</div>
<div class="form-group">
<label for="age">Age</label>
<input class="form-control" type="number" name="age" id="age" placeholder="##" value="<?php print($rowMember['age']); ?>" maxlength="2">
</div>
<div class="form-group">
<label for="ethnicity">Ethnicity</label>
<input class="form-control" type="text" name="ethnicity" id="ethnicity" placeholder="NL" value="<?php print($rowMember['ethnicity']); ?>" maxlength="2">
</div>
<div class="form-group">
<label for="gender">Gender</label>
<input class="form-control" type="text" name="gender" id="gender" placeholder="M" value="<?php print($rowMember['gender']); ?>" maxlength="1">
</div>
<div class="form-group">
<label for="party">Party</label>
<input class="form-control" type="text" name="party" id="party" placeholder="REP" value="<?php print($rowMember['party']); ?>" maxlength="3">
</div>
<div class="form-group">
<label for="race">Race</label>
<input class="form-control" type="text" name="race" id="race" placeholder="W" value="<?php print($rowMember['race']); ?>" maxlength="1">
</div>
<div class="form-group">
<label for="phone">Phone Number</label>
<input class="form-control" type="text" name="phone" id="phone" placeholder="###-###-#####" value="<?php print($rowMember['phone']); ?>" maxlength="12">
</div>
<input class="btn btn-primary mb-2" type="submit" name="btn_save" value="Submit">
</form>
</main>
</div>
</div>
<!-- Footer scripts, and functions -->
<?php require_once 'includes/footer.php'; ?>
</body>
</html>
<?php
// Website Homepage (where I'm having issues) - index.php
// Show PHP errors
ini_set('display_errors',1);
ini_set('display_startup_erros',1);
error_reporting(E_ALL);
require_once 'classes/member.php';
$objMember = new Member();
// GET
if(isset($_GET['delete_id'])){
$id = $_GET['delete_id'];
try{
if($id != null){
if($objMember->delete($id)){
$objMember->redirect('index.php?deleted');
}
}else{
var_dump($id);
}
}catch(PDOException $e){
echo $e->getMessage();
}
}
?>
<!doctype html>
<html lang="en">
<head>
<!-- Head metas, css, and title -->
<?php require_once 'includes/head.php'; ?>
</head>
<body>
<!-- Header banner -->
<?php require_once 'includes/header.php'; ?>
<div class="container-fluid">
<div class="row">
<!-- Sidebar menu -->
<?php require_once 'includes/sidebar.php'; ?>
<main role="main" class="col-md-9 ml-sm-auto col-lg-10 px-4">
<h1 style="margin-top: 10px">Members List</h1>
<?php
if(isset($_GET['updated'])){
echo '<div class="alert alert-info alert-dismissable fade show" role="alert">
<strong>User!<trong> Updated with success.
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true"> × </span>
</button>
</div>';
}else if(isset($_GET['deleted'])){
echo '<div class="alert alert-info alert-dismissable fade show" role="alert">
<strong>User!<trong> Deleted with success.
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true"> × </span>
</button>
</div>';
}else if(isset($_GET['inserted'])){
echo '<div class="alert alert-info alert-dismissable fade show" role="alert">
<strong>User!<trong> Inserted with success.
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true"> × </span>
</button>
</div>';
}else if(isset($_GET['error'])){
echo '<div class="alert alert-info alert-dismissable fade show" role="alert">
<strong>DB Error!<trong> Something went wrong with your action. Try again!
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true"> × </span>
</button>
</div>';
}
?>
<div class="table-responsive">
<table class="table table-striped table-sm">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Residential Address</th>
<th>Mailing Address</th>
<th>Precinct</th>
<th>Age</th>
<th>Ethnicity</th>
<th>Gender</th>
<th>Party</th>
<th>Race</th>
<th>Phone Number</th>
<th></th>
<th></th>
</tr>
</thead>
<?php
$query = "SELECT * FROM members LIMIT 25";
$stmt = $objMember->runQuery($query);
$stmt->execute();
?>
<tbody>
<?php if($stmt->rowCount() > 0){
while($rowMember = $stmt->fetch(PDO::FETCH_ASSOC)){
?>
<tr>
<td><?php print($rowMember['id']); ?></td>
<td><?php print($rowMember['name']); ?></td>
<td><?php print($rowMember['residential_address']); ?></td>
<td><?php print($rowMember['mailing_address']); ?></td>
<td><?php print($rowMember['precinct']); ?></td>
<td><?php print($rowMember['age']); ?></td>
<td><?php print($rowMember['ethnicity']); ?></td>
<td><?php print($rowMember['gender']); ?></td>
<td><?php print($rowMember['party']); ?></td>
<td><?php print($rowMember['race']); ?></td>
<td><?php print($rowMember['phone']); ?></td>
<td><a href="form.php?edit_id=<?php print($rowMember['id']); ?>"><span>Edit</span></a></td>
<td><a href="form.php?edit_id=<?php print($rowMember['id']); ?>"><span data-feather="trash"></span></a></td>
</tr>
</tbody>
<?php } } ?>
</table>
</div>
</main>
</div>
</div>
<!-- Footer scripts, and functions -->
<?php require_once 'includes/footer.php'; ?>
<!-- Custom scripts -->
<script>
// JQuery confirmation
$('.confirmation').on('click', function () {
return confirm('Are you sure you want to delete this member?');
});
</script>
</body>
</html>