Refactored, no more redirects. Improved error messaging system
This commit is contained in:
51
cable.php
51
cable.php
@@ -10,7 +10,8 @@ SPDX-License-Identifier: GPL-3.0-or-later
|
||||
include("includes.php");
|
||||
|
||||
if ($_SESSION['suser_role_admin'] == 0) {
|
||||
header_location('comments.php?comments=accessdenied');
|
||||
$g_error->add('Access denied!');
|
||||
$action = ACT_ERR_DENIED;
|
||||
}
|
||||
|
||||
if (isset($_REQUEST['id'])) {
|
||||
@@ -33,23 +34,43 @@ switch ($submit = form_get_action()) {
|
||||
|
||||
case 'insert':
|
||||
$description = sanitize($_POST['description']);
|
||||
$length = sanitize($_POST['length']);
|
||||
$color = sanitize($_POST['color']);
|
||||
$type = sanitize($_POST['cable_type']);
|
||||
$links = sanitize($_POST['links']);
|
||||
$info = sanitize($_POST['info']);
|
||||
$sql = "INSERT INTO cable
|
||||
(cable_description, cable_color, cable_type, cable_links, cable_info)
|
||||
(cable_description, cable_color, cable_type, cable_links,
|
||||
cable_length, cable_info)
|
||||
VALUES
|
||||
(:description, :color, :type, :links, :info)";
|
||||
(:description, :color, :type, :links,
|
||||
:length, :info)";
|
||||
$sth = $dbh->prepare($sql);
|
||||
$sth->bindValue(':description', $description, PDO::PARAM_STR);
|
||||
$sth->bindValue(':color', $color, PDO::PARAM_STR);
|
||||
$sth->bindValue(':type', $type, PDO::PARAM_STR);
|
||||
$sth->bindValue(':links', $info, PDO::PARAM_INT);
|
||||
$sth->bindValue(':info', $info, PDO::PARAM_STR);
|
||||
$sth->execute();
|
||||
$id = $dbh->lastInsertId();
|
||||
$action = ACT_VIEW;
|
||||
try {
|
||||
$sth->bindValue(':description', $description, PDO::PARAM_STR);
|
||||
$sth->bindValue(':length', $length, PDO::PARAM_INT);
|
||||
$sth->bindValue(':color', $color, PDO::PARAM_STR);
|
||||
$sth->bindValue(':type', $type, PDO::PARAM_STR);
|
||||
$sth->bindValue(':links', $info, PDO::PARAM_INT);
|
||||
$sth->bindValue(':info', $info, PDO::PARAM_STR);
|
||||
$sth->execute();
|
||||
$id = $dbh->lastInsertId();
|
||||
$action = ACT_VIEW;
|
||||
} catch (PDOException $e) {
|
||||
$g_error->Add($e->getMessage());
|
||||
if ($e->getCode() == 23000) {
|
||||
// duplicate key
|
||||
$g_warning->Add("Save failed");
|
||||
$g_warning->Add("Cable description '$description' already in use!");
|
||||
}
|
||||
// reassign entered values
|
||||
$smarty->assign('length', $length);
|
||||
$smarty->assign('type', $type);
|
||||
$smarty->assign('links', $links);
|
||||
$smarty->assign('color', $color);
|
||||
$smarty->assign('info', $info);
|
||||
$action = ACT_ADD;
|
||||
}
|
||||
break;
|
||||
|
||||
case 'update':
|
||||
@@ -156,6 +177,14 @@ $smarty->assign('description', $sth->fetchColumn());
|
||||
|
||||
$smarty->display('cabledel.tpl');
|
||||
|
||||
elseif ($action == ACT_ERR_DENIED):
|
||||
// ========== ERROR ACCESS TO PAGE DENIED =====================================
|
||||
|
||||
if (isset($_SERVER['HTTP_REFERER'])) {
|
||||
echo '<p"><a href="', $_SERVER['HTTP_REFERER'], '">', "Back to last page</a></p>\n";
|
||||
}
|
||||
echo "<p></p>";
|
||||
|
||||
else:
|
||||
// ========== ERROR UNKNOWN VARIANT ===========================================
|
||||
|
||||
|
||||
Reference in New Issue
Block a user