Skip to content

Instantly share code, notes, and snippets.

@clarade
Created March 22, 2021 09:56
Show Gist options
  • Save clarade/57474cc7009ff2789ec788e05de69a18 to your computer and use it in GitHub Desktop.
Save clarade/57474cc7009ff2789ec788e05de69a18 to your computer and use it in GitHub Desktop.
ODP exercises
<?php
require 'php-connec.php';
$pdo = new \PDO(DSN, USER, PASS);
$query = "SELECT * FROM friend";
$statement = $pdo->query($query);
$friends = $statement->fetchAll();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Friends</title>
</head>
<body>
<ul>
<?php
foreach ($friends as $friend) {
echo "<li>" . $friend['firstname'] . " " . $friend['lastname'] . "</li>";
};
?>
</ul>
<form action='script.php' method='post'>
<label for="firstName">First Name</label>
<input type="text" placeholder="Please enter a first name" name="firstName" id="firstName">
<label for="lastName">Last Name</label>
<input type="text" placeholder="Please enter a last name" name="lastName" id="lastName">
<button type="submit">Submit!</button>
</form>
</body>
</html>
<?php
require 'php-connec.php';
$pdo = new \PDO(DSN, USER, PASS);
$lastname = trim($_POST['lastName']);
$firstname = trim($_POST['firstName']);
$query = 'INSERT INTO friend (lastname, firstname) VALUES (:lastname, :firstname)';
$statement = $pdo->prepare($query);
$statement->bindValue(':lastname', $lastname, \PDO::PARAM_STR);
$statement->bindValue(':firstname', $firstname, \PDO::PARAM_STR);
$statement->execute();
header('Location:index.php');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment