Last active
December 5, 2015 22:30
-
-
Save julianjupiter/c92ae7c18645bb0f9926 to your computer and use it in GitHub Desktop.
PHP and MySQL with PDO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
try { | |
$connection = new PDO('mysql:host=localhost;dbname=jtest2', 'root', 'admin123'); | |
} catch (PDOException $e) { | |
echo $e->getMessage(); | |
} | |
$firstName = isset($_POST['first_name']) ? $_POST['first_name'] : ''; | |
$lastName = isset($_POST['last_name']) ? $_POST['last_name'] : ''; | |
$position = isset($_POST['position']) ? $_POST['position'] : ''; | |
$result = false; | |
$messageIncompleteData = ''; | |
$messageSuccess = ''; | |
if (isset($_POST['add'])) { | |
if ($firstName != '' && $lastName != '' && $position != '') { | |
try { | |
$queryInsert = 'INSERT INTO user(first_name, last_name, position, date_created) VALUES(:first_name, :last_name, :position, :date_created)'; | |
$statementInsert = $connection->prepare($queryInsert); | |
$result = $statementInsert->execute([':first_name' => $firstName, ':last_name' => $lastName, ':position' => $position, ':date_created' => date('y-m-d G:i:s')]); | |
} catch (PDOException $e) { | |
echo $e->getMessage(); | |
} | |
} else { | |
$messageIncompleteData = 'Please complete fields.'; | |
} | |
} | |
if ($result) { | |
$messageSuccess = 'Success!'; | |
$firstName = ''; | |
$lastName = ''; | |
$position = ''; | |
} | |
try { | |
$querySelect = 'SELECT id, first_name, last_name, position, date_created FROM user'; | |
$statementSelect = $connection->prepare($querySelect); | |
$statementSelect->execute(); | |
$data = $statementSelect->fetchAll(); | |
} catch (PDOException $e) { | |
echo $e->getMessage(); | |
} | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<title>PHP & MySQL</title> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"> | |
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --> | |
<!-- WARNING: Respond.js doesn't work if you view the page via file:// --> | |
<!--[if lt IE 9]> | |
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> | |
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> | |
<![endif]--> | |
</head> | |
<body style="padding-top:50px;"> | |
<div class="container"> | |
<div class="row"> | |
<div class="col-sm-4"> | |
<?php if (!empty($messageIncompleteData)) { ?> | |
<div class="alert alert-danger" role="alert"><?php echo $messageIncompleteData; ?></div> | |
<?php } if (!empty($messageSuccess)) { ?> | |
<div class="alert alert-success" role="alert"><?php echo $messageSuccess; ?></div> | |
<?php } ?> | |
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> | |
<div class="form-group"> | |
<label for="inputFirstName"><strong>First Name</strong></label> | |
<input type="text" class="form-control" id="inputFirstName" placeholder="First Name" value="<?php echo $firstName; ?>" name="first_name"> | |
</div> | |
<div class="form-group"> | |
<label for="inputLastName"><strong>Last Name</strong></label> | |
<input type="text" class="form-control" id="inputLastName" placeholder="Last Name" value="<?php echo $lastName; ?>" name="last_name"> | |
</div> | |
<div class="form-group"> | |
<label for="inputPosition"><strong>Position</strong></label> | |
<input type="text" class="form-control" id="inputFirstName" placeholder="Position" value="<?php echo $position; ?>" name="position"> | |
</div> | |
<button type="submit" class="btn btn-default" name="add">Submit</button> | |
</form> | |
</div> | |
<div class="col-sm-8"> | |
<table class="table table-hover"> | |
<thead> | |
<tr> | |
<th>ID</th> | |
<th>First Name</th> | |
<th>Last Name</th> | |
<th>Position</th> | |
<th>Date</th> | |
</tr> | |
</thead> | |
<tbody> | |
<?php | |
if (empty($data)) { | |
echo "<tr><td>No data available!</td></tr>"; | |
} else { | |
foreach ($data as $d) { | |
?> | |
<tr> | |
<td><?php echo $d['id']; ?></td> | |
<td><?php echo $d['first_name']; ?></td> | |
<td><?php echo $d['last_name']; ?></td> | |
<td><?php echo $d['position']; ?></td> | |
<td><?php echo date_format(date_create($d['date_created']), "m/d/Y"); ?></td> | |
</tr> | |
<?php | |
} | |
} | |
?> | |
</tbody> | |
</table> | |
</div> | |
</div> | |
</div> | |
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> | |
<!-- Include all compiled plugins (below), or include individual files as needed --> | |
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment