From cf270f3726a8037ebef80888eb510172688e13a7 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Fri, 30 Apr 2021 09:35:40 +0300 Subject: CRUD bugs --- bug/delete.php | 28 ++++++++++++++++++++++++++++ bug/edit.php | 45 +++++++++++++++++++++++++++------------------ bug/update.php | 31 +++++++++++++------------------ bug/view.php | 34 +++++++++++++++++++--------------- 4 files changed, 87 insertions(+), 51 deletions(-) (limited to 'bug') diff --git a/bug/delete.php b/bug/delete.php index e69de29..9054151 100644 --- a/bug/delete.php +++ b/bug/delete.php @@ -0,0 +1,28 @@ +prepare("DELETE FROM bugs WHERE id=:bug_id"); +$query->bindParam(':bug_id', $_GET['id']); +if($query->execute()) { + echo "

bug deleted

"; +} else { + echo "

bug failed to delete

"; +} + +redirect: header("Refresh: 2; URL=$_SERVER[HTTP_REFERER]"); +footer: require_once(TEMPLATES_PATH . "/footer.php"); +?> diff --git a/bug/edit.php b/bug/edit.php index 7eba339..ad1ea36 100644 --- a/bug/edit.php +++ b/bug/edit.php @@ -1,33 +1,42 @@ No bug selected, redirecting to index..."; - header('Refresh: 2; URL=index.php'); - -} else if(!isset($_SESSION['user_id']) || $_SESSION['user_id'] == "") { +if(!session_set()) { echo "
Not logged in, redirecting to index...
"; - header('Refresh: 2; URL=index.php'); + header("Refresh: 2; URL={$config['urls']['base']}"); + goto footer; +} + +$id = isset($_GET['id']) ? $_GET['id'] : ""; +$title = ""; +$submitter = $_SESSION['user_name']; +$description = ""; -} else { +if($id != "") { $conn = new PDO($config['db']['dsn'], $config['db']['username'], $config['db']['password']); + $query = $conn->prepare("SELECT title, description, users.username AS submitter FROM bugs + JOIN users ON bugs.author=users.id WHERE bugs.id=:bug_id"); + $query->bindParam(':bug_id', $id); + $query->execute(); - $query = "SELECT title, description, users.username AS submitter FROM bugs JOIN users ON bugs.author = users.id WHERE bugs.id=$_GET[id]"; - $result = $conn->query($query)->fetch(); + $result = $query->fetch(); + $title = $result['title']; + $submitter = $result['submitter']; + $description = $result['description']; +} ?> -
-

Title:

-

Submitted by:

-

Description:

- + + +

Title:

+

Submitted by:

+

Description:

+
diff --git a/bug/update.php b/bug/update.php index f27cdd4..0ee6e26 100644 --- a/bug/update.php +++ b/bug/update.php @@ -1,35 +1,30 @@ No bug selected, redirecting to index..."; - header('Refresh: 2; URL=index.php'); - -} else if(!isset($_SESSION['user_id']) || $_SESSION['user_id'] == "") { - echo "
Not logged in, redirecting to index...
"; - header('Refresh: 2; URL=index.php'); - -} else { +if(session_set()) { $conn = new PDO($config['db']['dsn'], $config['db']['username'], $config['db']['password']); - - $query = $conn->prepare("UPDATE bugs SET title=:title, description=:description WHERE id=:id"); + $query = $_POST['id'] == "" ? + $conn->prepare("INSERT INTO bugs (author, title, description) VALUES (:user_id, :title, :description)") + : $conn->prepare("UPDATE bugs SET title=:title, description=:description WHERE id=:bug_id"); + if($_POST['id'] == "") { + $query->bindParam(':user_id', $_SESSION['user_id']); + } else { + $query->bindParam(':bug_id', $_POST['id']); + } $query->bindParam(':title', $_POST['title']); $query->bindParam(':description', $_POST['description']); - $query->bindParam(':id', $_GET['id']); if ($query->execute()) { echo "Data is updated\n"; } else { - echo "User must have sent wrong inputs\n"; + echo "Query failed\n"; } - - header("Refresh: 2; URL=view.php?id=$_GET[id]"); - } -require_once(TEMPLATES_PATH . "/footer.php"); +header("Refresh: 2; URL=$_SERVER[HTTP_REFERER]"); +footer: require_once(TEMPLATES_PATH . "/footer.php"); ?> diff --git a/bug/view.php b/bug/view.php index 2468abb..d1e4335 100644 --- a/bug/view.php +++ b/bug/view.php @@ -1,28 +1,32 @@ No bug selected, redirecting to index..."; - header('Refresh: 2; URL=index.php'); + echo "
No bug selected...
"; + header("Refresh: 2; URL=$_SERVER[HTTP_REFERER]"); + goto footer; +} -} else { - if(isset($_SESSION['user_id']) && $_SESSION['user_id'] != "") { - echo "

Edit

"; - } +$conn = new PDO($config['db']['dsn'], $config['db']['username'], $config['db']['password']); +$query = $conn->prepare("SELECT title, description, users.username AS submitter FROM bugs + JOIN users ON bugs.author = users.id WHERE bugs.id=:bug_id"); +$query->bindParam(':bug_id', $_GET['id']); +$query->execute(); +$result = $query->fetch(); - $conn = new PDO($config['db']['dsn'], $config['db']['username'], $config['db']['password']); - $markdown = new Parsedown(); +$markdown = new Parsedown(); - $query = "SELECT title, description, users.username AS submitter FROM bugs JOIN users ON bugs.author = users.id WHERE bugs.id=$_GET[id]"; - $result = $conn->query($query)->fetch(); +echo "

$result[title]

"; +echo "

Submitted by $result[submitter]

"; +echo '

' . $markdown->text($result['description']) . '

'; - echo "

$result[title]

"; - echo "

Submitted by $result[submitter]

"; - echo '

' . $markdown->text($result['description']) . '

'; +if(session_set()) { + echo "

Edit | Delete

"; } -require_once(TEMPLATES_PATH . "/footer.php"); +footer: require_once(TEMPLATES_PATH . "/footer.php"); ?> -- cgit v1.2.1