created basic DB structure and accessing db

This commit is contained in:
Joshua Ashton
2025-03-05 18:55:04 -07:00
parent 8d244e586d
commit 54228c4e8e
16 changed files with 151 additions and 16 deletions
Regular → Executable
View File
Regular → Executable
View File
Generated Regular → Executable
View File
Regular → Executable
View File
Regular → Executable
View File
Regular → Executable
View File
Regular → Executable
View File
Regular → Executable
View File
Regular → Executable
View File
+14
View File
@@ -0,0 +1,14 @@
<?php
$nav = '
<nav>
<ul>
<li><a href="home.php">Home</a></li>
<li><a href="account.php">Account</a></li>
<li><a href="admin.php">Admin</a></li>
<li><a href="articles.php">Articles</a></li>
</ul>
</nav>
';
echo $nav;
Regular → Executable
+4
View File
@@ -0,0 +1,4 @@
<?php
include_once ('includes/sql.php');
function login($username, $password_hash) {}
Regular → Executable
+11 -10
View File
@@ -5,11 +5,11 @@ $dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/..');
$dotenv->safeLoad(); $dotenv->safeLoad();
// Use environment variables for the database password and IP address. // Use environment variables for the database password and IP address.
// $db_user = $_ENV['DATABASE_USER']; $db_user = $_ENV['DATABASE_USER'];
$db_pass = $_ENV['DATABASE_PASSWORD']; $db_pass = $_ENV['DATABASE_PASSWORD'];
define('HOST', 'localhost'); define('HOST', 'localhost');
define('USER', 'root'); define('USER', $db_user);
define('PASS', $db_pass); define('PASS', $db_pass);
define('DB', 'vintagecodingdotnet'); define('DB', 'vintagecodingdotnet');
@@ -17,12 +17,13 @@ function get_article_cards()
{ {
$conn = mysqli_connect(HOST, USER, PASS, DB); $conn = mysqli_connect(HOST, USER, PASS, DB);
/* $query = 'SELECT * FROM accounts;';
* $query = 'SELECT * FROM articles;';
* $results = mysqli_query($conn, $query);
* $results = mysqli_query($conn, $query); echo '<pre>';
* while ($row = mysqli_fetch_row($results)) { while ($row = mysqli_fetch_array($results)) {
* echo $row['article_name']; print_r($row);
* } echo '<br>';
*/ }
echo '</pre>';
} }
Regular → Executable
+5 -3
View File
@@ -1,5 +1,7 @@
<?php <?php
include_once ('includes/components/head.php'); include_once('includes/components/head.php');
include_once ('includes/sql.php'); include_once('includes/components/nav.php');
include_once('includes/sql.php');
get_article_cards(); get_article_cards();
include_once ('includes/components/foot.php'); include_once('includes/components/foot.php');
Regular → Executable
View File
Regular → Executable
View File
+117 -3
View File
@@ -1,7 +1,121 @@
create_db() { create_db() {
mariadb -u root -p -e "CREATE DATABASE vintagecodingdotnet;" /opt/lampp/bin/mariadb -u violet -p -e "DROP DATABASE IF EXISTS vintagecodingdotnet;";
mariadb -u root -p -e "USE vintagecodingdotnet; CREATE TABLE articles (article_id INT PRIMARY KEY AUTO_INCREMENT, article_name VARCHAR(255) NOT NULL);" /opt/lampp/bin/mariadb -u violet -p -e "CREATE DATABASE vintagecodingdotnet;";
mariadb -u root -p -e "USE vintagecodingdotnet; INSERT INTO articles (article_name) VALUES ('test'), ('another test'), ('a third test');"
# Create file_types table.
/opt/lampp/bin/mariadb -u violet -p -e "
USE vintagecodingdotnet;
CREATE TABLE file_types (
file_type_id INT PRIMARY KEY AUTO_INCREMENT,
file_type VARCHAR(8)
);";
# Create the media table.
/opt/lampp/bin/mariadb -u violet -p -e "
USE vintagecodingdotnet;
CREATE TABLE media (
media_id INT PRIMARY KEY,
file_name TINYTEXT NOT NULL,
file_path VARCHAR(2048) NOT NULL,
file_type INT NOT NULL,
FOREIGN KEY (file_type) REFERENCES file_types(file_type_id)
);";
# Create the roles table.
/opt/lampp/bin/mariadb -u violet -p -e "
USE vintagecodingdotnet;
CREATE TABLE roles (
role_id INT PRIMARY KEY AUTO_INCREMENT,
role VARCHAR(32) NOT NULL
);";
# Create the accounts table.
/opt/lampp/bin/mariadb -u violet -p -e "
USE vintagecodingdotnet;
CREATE TABLE accounts (
account_id INT PRIMARY KEY,
username VARCHAR(32) UNIQUE NOT NULL,
email VARCHAR(64) UNIQUE NOT NULL,
password_hash VARCHAR(64) NOT NULL,
role INT NOT NULL,
registration_date DATETIME,
bio TINYTEXT,
profile_picture INT,
FOREIGN KEY (role) REFERENCES roles(role_id),
FOREIGN KEY (profile_picture) REFERENCES media(media_id)
);";
# Create the article_status table.
/opt/lampp/bin/mariadb -u violet -p -e "
USE vintagecodingdotnet;
CREATE TABLE article_status (
status_id INT PRIMARY KEY AUTO_INCREMENT,
status VARCHAR(16) NOT NULL
);";
# Create the articles table.
/opt/lampp/bin/mariadb -u violet -p -e "
USE vintagecodingdotnet;
CREATE TABLE articles (
article_id INT PRIMARY KEY AUTO_INCREMENT,
account_id INT NOT NULL,
title VARCHAR(128) UNIQUE NOT NULL,
slug VARCHAR(128) UNIQUE NOT NULL,
content LONGTEXT NOT NULL,
publish_date DATETIME,
last_modified DATETIME NOT NULL,
status INT NOT NULL,
featured_image INT,
excerpt VARCHAR(255),
FOREIGN KEY (account_id) REFERENCES accounts(account_id),
FOREIGN KEY (status) REFERENCES article_status(status_id)
);";
}
# Insert data constants into the database.
# Acceptable file types: png, jpg, jpeg, svg.
# Acceptable roles: administrator, contributor, commenter, guest.
# Acceptable article status: published, draft, archived.
#
# Also creates administrator user
const_data() {
# Insert the data to the file_types table.
# Acceptable file types are png, jpg, jpeg, svg
/opt/lampp/bin/mariadb -u violet -p -e "
USE vintagecodingdotnet;
INSERT INTO file_types (file_type) VALUES ('png'), ('jpg'), ('jpeg'), ('svg');
";
# Insert the data to the roles table.
/opt/lampp/bin/mariadb -u violet -p -e "
USE vintagecodingdotnet;
INSERT INTO roles (role) VALUES ('administrator'), ('contributor'), ('commenter'), ('guest');
";
# Insert the data to the article_status table.
/opt/lampp/bin/mariadb -u violet -p -e "
USE vintagecodingdotnet;
INSERT INTO article_status (status) VALUES ('published'), ('draft'), ('archived');
";
# Create an administrator user.
/opt/lampp/bin/mariadb -u violet -p -e "
USE vintagecodingdotnet;
INSERT INTO accounts
(account_id, username, email, password_hash, role, registration_date, bio, profile_picture) VALUES
(42, 'admin', 'me@joshashton.dev', 'e03b1a00caa6e8113a3bc3e7101ecbd0fea710f5e091e7b195bdba53bbeb004e', 1, CURDATE(), null, null);
";
}
insert_data() {
/opt/lampp/bin/mariadb -u violet -p -e "
USE vintagecodingdotnet;
INSERT INTO articles (article_name) VALUES
('test'),
('another test'),
('a third test');
";
} }
create_db create_db
const_data