Rough Draft of SQL Implmentation in PHP

This commit is contained in:
Evan G. 2024-06-09 14:49:18 -05:00
parent f8c930c1bd
commit 683bca276e
2 changed files with 94 additions and 1 deletions

View File

@ -16,7 +16,7 @@
<a href="../index.html">Back Home</a> <a href="../index.html">Back Home</a>
</nav> </nav>
<main> <main>
<form action="./submit.php" method="post"> <form action="./restore.php" method="post">
<input type="text" name="fname" placeholder="name"></input> <input type="text" name="fname" placeholder="name"></input>
<input type="text" name="fmessage" placeholder="Message"></input> <input type="text" name="fmessage" placeholder="Message"></input>
<input type="submit"></input> <input type="submit"></input>

93
restore.php Normal file
View File

@ -0,0 +1,93 @@
<?php
# Setup Data Object
class data {
public $name;
public $message;
public $date;
public $mlength;
public $nlength;
function set_date() {
$this->date = date("Y M d");
}
function set_mlength() {
$this->mlength = strlen($this->message);
}
function set_nlength() {
$this->nlength = strlen($this->name);
}
}
if (!isset($_SERVER["HTTP_HOST"])) {
parse_str($argv[1], $_POST);
}
$ndata = new data();
$name = $ndata->name = htmlspecialchars($_POST["fname"]);
$message = $ndata->message = htmlspecialchars($_POST["fmessage"]);
$ndata->set_date();
$ndata->set_mlength();
$ndata->set_nlength();
$mlength = $ndata->mlength;
$nlength = $ndata->nlength;
$newURL = "https://" . $_SERVER['SERVER_NAME'] . "/guestbook/index.html";
# Check name and message
if ($name == "") {
header('Refresh:2; url=' . $newURL );
echo "There is no name, Try again with a name";
exit();
}
if ($message == "") {
header('Refresh:2; url=' . $newURL );
echo "There is no message, Try again with a message";
exit();
}
if($mlength > 200) {
header('Refresh:2; url=' . $newURL );
echo "This message is too long";
exit();
}
if($nlength > 25 ) {
header('Refresh:2; url=' . $newURL );
echo "The name is too long";
exit();
}
$dbh = new PDO('sqlite:/home/evan/Development/Website-Redesign/static/guestbook/database.sq3');
function setupdb($dbh) {
$dbh->exec('CREATE TABLE IF NOT EXISTS messages (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
date TEXT NOT NULL,
message TEXT NOT NULL,
mlength INTEGER NOT NULL,
nlength INTEGER NOT NULL
)');
}
function additem($ndata) {
$sql = 'INSERT INTO messages
(id, name, date, message, mlength, nlength)
VALUES (?, ?, ?, ?, ?, ?)';
$dbh = new PDO('sqlite:/home/evan/Development/Website-Redesign/static/guestbook/database.sq3');
$sth = $dbh->prepare($sql);
$sth->execute(array(
null->id,
$ndata->name,
$ndata->date,
$ndata->message,
$ndata->mlength,
$ndata->nlength
));
// Commit the Changes
// $dbh->commit();
}
setupdb($dbh);
additem($ndata);
print_r($ndata);
print($ndata->name);
?>