Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active May 26, 2023 03:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/fedbf232e1b1b10ae0953db79479fbd1 to your computer and use it in GitHub Desktop.
Save code-boxx/fedbf232e1b1b10ae0953db79479fbd1 to your computer and use it in GitHub Desktop.
How to develop a PHP MYSQL web app - Simple example.

SIMPLE PHP MYSQL WEB APP EXAMPLE

https://code-boxx.com/simple-php-web-application/

NOTES

  1. Create a database and import the 1-database.sql file.
  2. Change the database settings in 2-todo-lib.php to your own.
  3. Launch 3a-todo.php in your browser.

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

CREATE TABLE `todo` (
`todo_id` bigint(20) NOT NULL,
`todo_task` text NOT NULL,
`todo_status` tinyint(1) NOT NULL DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `todo`
ADD PRIMARY KEY (`todo_id`);
ALTER TABLE `todo`
MODIFY `todo_id` bigint(20) NOT NULL AUTO_INCREMENT;
<?php
class ToDo {
// (A) CONSTRUCTOR - CONNECT TO DATABASE
private $pdo = null;
private $stmt = null;
public $error = "";
function __construct () {
$this->pdo = new PDO(
"mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset=".DB_CHARSET,
DB_USER, DB_PASSWORD, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
]);
}
// (B) DESTRUCTOR - CLOSE DATABASE CONNECTION
function __destruct () {
if ($this->stmt!==null) { $this->stmt = null; }
if ($this->pdo!==null) { $this->pdo = null; }
}
// (C) HELPER - RUN SQL QUERY
function query ($sql, $data=null) : void {
$this->stmt = $this->pdo->prepare($sql);
$this->stmt->execute($data);
}
// (D) SAVE TO-DO TASK
function save ($task, $status, $id=null) {
if ($id===null) {
$sql = "INSERT INTO `todo` (`todo_task`, `todo_status`) VALUES (?,?)";
$data = [$task, $status];
} else {
$sql = "UPDATE `todo` SET `todo_task`=?, `todo_status`=? WHERE `todo_id`=?";
$data = [$task, $status, $id];
}
$this->query($sql, $data);
return true;
}
// (E) GET ALL TASKS
function getAll () {
$this->query("SELECT * FROM `todo`");
return $this->stmt->fetchAll();
}
// (F) DELETE TASK
function del ($id) {
$this->query("DELETE FROM `todo` WHERE `todo_id`=?", [$id]);
return true;
}
}
// (G) DATABASE SETTINGS - CHANGE TO YOUR OWN!
define("DB_HOST", "localhost");
define("DB_NAME", "test");
define("DB_CHARSET", "utf8mb4");
define("DB_USER", "root");
define("DB_PASSWORD", "");
// (H) NEW TO-DO OBJECT
$TODO = new ToDo();
<!DOCTYPE html>
<html>
<head>
<title>Simple To Do List</title>
<meta charset="utf-8">
<link rel="stylesheet" href="3b-todo.css">
<script src="3c-todo.js"></script>
</head>
<body>
<?php
// (A) ADD/UPDATE/DELETE TASK IF FORM SUBMITTED
require "2-todo-lib.php";
if (isset($_POST["action"])) {
// (A1) SAVE TASK
if ($_POST["action"]=="save") {
$pass = $TODO->save($_POST["task"], $_POST["status"], (isset($_POST["id"])?$_POST["id"]:null));
}
// (A2) DELETE TASK
else { $pass = $TODO->del($_POST["id"]); }
// (A3) SHOW RESULT
echo "<div class='notify'>";
echo $pass ? "OK" : $TODO->error ;
echo "</div>";
}
?>
<!-- (B) NINJA DELETE FORM -->
<form id="ninForm" method="post">
<input type="hidden" name="action" value="del">
<input type="hidden" name="id" id="ninID">
</form>
<div id="tasks">
<!-- (C) ADD NEW TASK -->
<form method="post">
<input type="hidden" name="action" value="save">
<input type="text" id="taskadd" name="task" placeholder="Task" required>
<select name="status">
<option value="0">Pending</option>
<option value="1">Done</option>
<option value="2">Canceled</option>
</select>
<input type="submit" value="Add">
</form>
<!-- (D) LIST TASKS -->
<?php
$tasks = $TODO->getAll();
if (count($tasks)!=0) { foreach ($tasks as $t) { ?>
<form method="post">
<input type="button" value="X" onclick="deltask(<?=$t["todo_id"]?>)">
<input type="hidden" name="action" value="save">
<input type="hidden" name="id" value="<?=$t["todo_id"]?>">
<input type="text" name="task" placeholder="Task" value="<?=$t["todo_task"]?>">
<select name="status">
<option value="0"<?=$t["todo_status"]==0?" selected":""?>>Pending</option>
<option value="1"<?=$t["todo_status"]==1?" selected":""?>>Done</option>
<option value="2"<?=$t["todo_status"]==2?" selected":""?>>Canceled</option>
</select>
<input type="submit" value="Save">
</form>
<?php }} ?>
</div>
</body>
</html>
* {
font-family: Arial, Helvetica, sans-serif;
box-sizing: border-box;
}
.notify {
background: #ecefff;
border: 1px solid #bfc4ff;
padding: 5px;
margin-bottom: 10px;
}
#tasks { max-width: 500px; }
#tasks form {
display: grid;
grid-template-columns: 10% 60% 20% 10%;
padding: 10px;
background: #f7f7f7;
border: 1px solid #ccc;
margin-bottom: 10px;
}
#tasks input, #tasks select {
padding: 10px;
cursor: pointer;
border: 0;
}
#tasks input[type=button], #tasks input[type=submit] { color: #fff; }
#tasks input[type=button] { background: #a72020; }
#tasks input[type=submit] { background: #577ed8; }
#taskadd { grid-column: span 2; }
function deltask (id) { if (confirm("Delete task?")) {
document.getElementById("ninID").value = id;
document.getElementById("ninForm").submit();
}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment