<form method="post">
<textarea rows="4" cols="50" name="Idea" maxlength="255" placeholder="Enter your Idea Here!!"></textarea><br/>
<input type="text" name="Name" value="" placeholder="Enter your Name Here!!"/>
<input type="submit" />
</form>
<?php
$dbname = 'Ideas.sqlite';
$exists = true;
if (! file_exists($dbname) ) {
touch($dbname);
chmod($dbname, 0660);
$exists = false;
}
function no_results() {
echo "No Ideas - Posted yet!!";
exit;
}
try {
$db = new PDO("sqlite:{$dbname}");
if ($exists === false) {
$db->exec("CREATE TABLE Ideas (Id INTEGER PRIMARY KEY, Idea TEXT, Name TEXT, Done INTEGER)");
}
if (isset($_POST['Idea']) && !empty($_POST['Idea'])) {
$name = (isset($_POST['Name']) && !empty($_POST['Name'])) ? ucwords($_POST['Name']) : 'Guest';
$insert = $db->prepare("INSERT INTO Ideas (Idea, Name, Done) VALUES (:Idea, :Name, 0);");
$insert->bindParam(':Idea', $_POST['Idea'], PDO::PARAM_STR);
$insert->bindParam(':Name', $name, PDO::PARAM_STR);
$insert->execute();
}
if (isset($_GET['DoneID']) && intval($_GET['DoneID']) > 0) {
$id = intval($_GET['DoneID']);
$complete = $db->prepare("UPDATE Ideas SET Done=1 WHERE Id=:Id LIMIT 1;");
$complete->bindParam(':Id', $id, PDO::PARAM_INT);
$complete->execute();
}
$result = $db->query('SELECT Id, Idea, Name FROM Ideas WHERE Done=0');
if ($result === false) {
no_results();
}
$rows = $result->fetchAll(PDO::FETCH_ASSOC);
if (count($rows) === 0) {
no_results();
}
echo "<table border=1>";
$mark = (isset($_GET['unlock'])) ? '<td>Mark as Complete</td>' : '';
echo "<tr><td>Idea#</td><td>Idea</td><td>by Name</td>{$mark}</tr>";
foreach ($rows as $row) {
echo "<tr><td>" . $row['Id'] . "</td>";
echo "<td>" . htmlentities($row['Idea'], ENT_QUOTES) . "</td>";
echo "<td>" . htmlentities($row['Name'], ENT_QUOTES) . "</td>";
if (isset($_GET['unlock'])) {
echo "<td><a href=\"?DoneID=" . $row['Id'] . "\">Done</a></td></tr>";
}
}
echo "</table>";
$db = NULL;
} catch (PDOException $e) {
echo 'Exception : ' . $e->getMessage();
}