Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5bb1637

Browse files
Initial additions
1 parent 582e596 commit 5bb1637

File tree

9 files changed

+443
-0
lines changed

9 files changed

+443
-0
lines changed

‎README.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# PHP with PDO (PHP Data Objects) Quickstart
2+
3+
This repository contains a simple web application that demonstrates how to quickly connect to and communicate with a [MariaDB](https://mariadb.com) database using [PHP](https://www.php.net/) and [PDO (PHP Data Objects)](https://www.php.net/manual/en/book.pdo.php).
4+
5+
<p align="center" spacing="10">
6+
<kbd>
7+
<img src="media/demo.gif" />
8+
</kbd>
9+
</p>
10+
11+
## Getting Started
12+
13+
The application in this repository demonstrates how to:
14+
15+
* Connect to a MariaDB database using [PDO (PHP Data Objects)](https://www.php.net/manual/en/book.pdo.php)
16+
* Execute queries (`SELECT`, `UPDATE`, `INSERT` and `DELETE`) to manage _contact_ data (like a digital [rolodex](https://en.wikipedia.org/wiki/Rolodex))
17+
* Use prepared statements
18+
19+
### Prepare the database
20+
21+
The application relies on a single database (`rolodex`) that contains a single table (`contacts`). You can find the necessary SQL for setting up the environment in [schema.sql](schema.sql).
22+
23+
### Run the application
24+
25+
After you've [pulled down this repository](https://git-scm.com/docs/git-clone), follow these steps to get the app up and running:
26+
27+
1. Update the database configuration settings in [src/config.php](src/config.php) (which is used across the app) to point to _your_ MariaDB database.
28+
29+
_Example configuration:_
30+
31+
```php
32+
$dsn = "mysql:host=127.0.0.1;dbname=rolodex;charset=utf8mb4";
33+
34+
$options = [
35+
PDO::ATTR_EMULATE_PREPARES => false, // Disable emulation mode for "real" prepared statements
36+
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // Disable errors in the form of exceptions
37+
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, // Make the default fetch be an associative array
38+
];
39+
40+
$pdo = new PDO($dsn, "app_user", "Password123!", $options);
41+
```
42+
43+
2. Within the [src](src) directory, run the application using the [built-in web server](https://www.php.net/manual/en/features.commandline.webserver.php).
44+
45+
```bash
46+
$ php -S localhost:5000
47+
```
48+
49+
## Helpful Resources
50+
51+
* [MariaDB Quickstart](https://github.com/mariadb-developers/mariadb-getting-started)
52+
* [Official MariaDB Documentation](https://mariadb.com/docs)
53+
54+
## Support and Contribution
55+
56+
Please feel free to submit PR's, issues or requests to this project directly.
57+
58+
If you have any other questions, comments, or looking for more information on MariaDB please check out:
59+
60+
* [MariaDB Developer Hub](https://mariadb.com/developers)
61+
* [MariaDB Community Slack](https://r.mariadb.com/join-community-slack)
62+
63+
Or reach out to us directly via:
64+
65+
* [developers@mariadb.com](mailto:developers@mariadb.com)
66+
* [MariaDB Twitter](https://twitter.com/mariadb)
67+
68+
## License <a name="license"></a>
69+
[![License](https://img.shields.io/badge/License-MIT-blue.svg?style=plastic)](https://opensource.org/licenses/MIT)

‎media/demo.gif

418 KB
Loading[フレーム]

‎schema.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CREATE DATABASE `rolodex`;
2+
3+
CREATE TABLE `rolodex`.`contacts` (
4+
`id` INT(11) NOT NULL AUTO_INCREMENT,
5+
`name` VARCHAR(100) NOT NULL,
6+
`age` INT(3) NOT NULL,
7+
`email` VARCHAR(100) NOT NULL,
8+
PRIMARY KEY (`id`)
9+
);

‎src/add.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
// Include database connection file
3+
include_once("config.php");
4+
5+
if(isset($_POST['update']))
6+
{
7+
// Retrieve record values
8+
$name = $_POST['name'];
9+
$age = $_POST['age'];
10+
$email = $_POST['email'];
11+
12+
$nameErr = $ageErr = $emailErr = "";
13+
14+
// Check for empty fields
15+
if(empty($name) || empty($age) || empty($email)) {
16+
if(empty($name)) {
17+
$nameErr = "* required";
18+
}
19+
if(empty($age)) {
20+
$ageErr = "* required";
21+
}
22+
if(empty($email)) {
23+
$emailErr = "* required";
24+
}
25+
} else {
26+
// Insert new contact
27+
$stmt = $pdo->prepare("INSERT INTO contacts (name,age,email) VALUES(?, ?, ?)");
28+
$stmt->execute([$name, $age, $email]);
29+
30+
// Redirect to home page (index.php)
31+
header("Location: index.php");
32+
}
33+
}
34+
else if (isset($_POST['cancel'])) {
35+
// Redirect to home page (index.php)
36+
header("Location: index.php");
37+
}
38+
?>
39+
<html>
40+
<head>
41+
<title>Edit Contact</title>
42+
<link rel="stylesheet" href="styles.css" />
43+
</head>
44+
<body>
45+
<form name="form1" method="post" action="add.php">
46+
<table>
47+
<tr>
48+
<td>Name</td>
49+
<td>
50+
<input type="text" name="name" value="<?php echo $name;?>">
51+
<span class="error"><?php echo $nameErr;?></span>
52+
</td>
53+
</tr>
54+
<tr>
55+
<td>Age</td>
56+
<td>
57+
<input type="text" name="age" value="<?php echo $age;?>">
58+
<span class="error"><?php echo $ageErr;?></span>
59+
</td>
60+
</tr>
61+
<tr>
62+
<td>Email</td>
63+
<td>
64+
<input type="text" name="email" value="<?php echo $email;?>">
65+
<span class="error"><?php echo $emailErr;?></span>
66+
</td>
67+
</tr>
68+
<tr>
69+
<td>
70+
<input class="cancel" type="submit" name="cancel" value="Cancel">
71+
</td>
72+
<td>
73+
<input type="submit" name="update" value="Update">
74+
</td>
75+
</tr>
76+
</table>
77+
</form>
78+
</body>
79+
</html>

‎src/config.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
$dsn = "mysql:host=<insert_host_address_here>;dbname=rolodex;charset=utf8mb4";
4+
5+
$options = [
6+
PDO::ATTR_EMULATE_PREPARES => false, // Disable emulation mode for "real" prepared statements
7+
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // Disable errors in the form of exceptions
8+
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, // Make the default fetch be an associative array
9+
];
10+
11+
try {
12+
$pdo = new PDO($dsn, "<insert_user_here>", "<insert_password_here>", $options);
13+
}
14+
catch (Exception $e) {
15+
error_log($e->getMessage());
16+
exit('Something bad happened');
17+
}
18+
19+
?>

‎src/delete.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
// Include database connection file
3+
include("config.php");
4+
5+
// Retrieve [id] value from querystring parameter
6+
$id = $_GET['id'];
7+
8+
// Delete row for a specified [id]
9+
$stmt = $pdo->prepare("DELETE FROM contacts WHERE id = ?");
10+
$stmt->execute([$id]);
11+
12+
// Redirect to home page (index.php)
13+
header("Location:index.php");
14+
?>

‎src/edit.php

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
// Include database connection file
3+
include_once("config.php");
4+
5+
if(isset($_POST['update']))
6+
{
7+
// Retrieve record values
8+
$id = $_POST['id'];
9+
$name = $_POST['name'];
10+
$age = $_POST['age'];
11+
$email = $_POST['email'];
12+
13+
$nameErr = $ageErr = $emailErr = "";
14+
15+
// Check for empty fields
16+
if(empty($name) || empty($age) || empty($email)) {
17+
if(empty($name)) {
18+
$nameErr = "* required";
19+
}
20+
if(empty($age)) {
21+
$ageErr = "* required";
22+
}
23+
if(empty($email)) {
24+
$emailErr = "* required";
25+
}
26+
} else {
27+
// Execute UPDATE
28+
$stmt = $pdo->prepare("UPDATE contacts SET name = ?, age = ?, email = ? WHERE id = ?");
29+
$stmt->execute([$name, $age, $email, $id]);
30+
31+
// Redirect to home page (index.php)
32+
header("Location: index.php");
33+
}
34+
}
35+
else if (isset($_POST['cancel'])) {
36+
// Redirect to home page (index.php)
37+
header("Location: index.php");
38+
}
39+
?>
40+
<?php
41+
//class Contact
42+
43+
// Retrieve id value from querystring parameter
44+
$id = $_GET['id'];
45+
46+
// Get contact by id
47+
$stmt = $pdo->prepare("SELECT * FROM contacts WHERE id = ?");
48+
$stmt->execute([$id]);
49+
$arr = $stmt->fetch(PDO::FETCH_ASSOC);
50+
51+
if (!$arr) {
52+
printf($arr);
53+
exit($arr);
54+
}
55+
else {
56+
//foreach($arr as $row)
57+
//{
58+
$name = $arr['name'];
59+
$age = $arr['age'];
60+
$email = $arr['email'];
61+
//}
62+
}
63+
?>
64+
<html>
65+
<head>
66+
<title>Edit Contact</title>
67+
<link rel="stylesheet" href="styles.css" />
68+
</head>
69+
<body>
70+
<form name="form1" method="post" action="edit.php?id=<?php echo $id ?>">
71+
<table>
72+
<tr>
73+
<td>Name</td>
74+
<td>
75+
<input type="text" name="name" value="<?php echo $name;?>">
76+
<span class="error"><?php echo $nameErr;?></span>
77+
</td>
78+
</tr>
79+
<tr>
80+
<td>Age</td>
81+
<td>
82+
<input type="text" name="age" value="<?php echo $age;?>">
83+
<span class="error"><?php echo $ageErr;?></span>
84+
</td>
85+
</tr>
86+
<tr>
87+
<td>Email</td>
88+
<td>
89+
<input type="text" name="email" value="<?php echo $email;?>">
90+
<span class="error"><?php echo $emailErr;?></span>
91+
</td>
92+
</tr>
93+
<tr>
94+
<td>
95+
<input class="cancel" type="submit" name="cancel" value="Cancel">
96+
</td>
97+
<td>
98+
<input type="submit" name="update" value="Update">
99+
<input type="hidden" name="id" value=<?php echo $_GET['id'];?>>
100+
</td>
101+
</tr>
102+
</table>
103+
</form>
104+
</body>
105+
</html>

‎src/index.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
// Include the database connection file
3+
include_once("config.php");
4+
5+
class Contact {
6+
public $id;
7+
public $name;
8+
public $age;
9+
public $email;
10+
}
11+
12+
// Fetch contacts (in descending order)
13+
$contacts = $pdo->query( "SELECT * FROM contacts ORDER BY id DESC")->fetchAll(PDO::FETCH_CLASS, 'Contact');
14+
15+
?>
16+
<html>
17+
<head>
18+
<title>MariaDB Rolodex</title>
19+
<link rel="stylesheet" href="styles.css" />
20+
</head>
21+
<body>
22+
<table>
23+
<tr>
24+
<td>Name</td>
25+
<td>Age</td>
26+
<td>Email</td>
27+
<td><a class="button" href="add.php">Add Contact</a></td>
28+
</tr>
29+
<?php
30+
// Print contacts
31+
foreach($contacts as $contact) {
32+
echo "<tr>";
33+
echo "<td>".$contact->name."</td>";
34+
echo "<td>".$contact->age."</td>";
35+
echo "<td>".$contact->email."</td>";
36+
echo "<td><a href=\"edit.php?id=$contact->id\">Edit</a> | <a href=\"delete.php?id=$contact->id\" onClick=\"return confirm('Are you sure you want to delete this contact?')\">Delete</a></td>";
37+
}
38+
?>
39+
</table>
40+
</body>
41+
</html>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /