Skip to content

Commit

Permalink
improved error messages in all try..catch blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
torbengb committed Oct 1, 2020
1 parent 26e1b96 commit cdb9485
Show file tree
Hide file tree
Showing 18 changed files with 202 additions and 60 deletions.
10 changes: 10 additions & 0 deletions common/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,13 @@ function escape($html) {
return htmlspecialchars($html, ENT_QUOTES | ENT_SUBSTITUTE, "UTF-8");
}

function showMessage($line = 0 , $file = "unknown" , $message = "") {
// usage: showMessage( __LINE__ , __FILE__ , "optional hint message" )
// or: showMessage( __LINE__ , __FILE__ )
echo "An error occurred at line " . $line . " in file " . $file . "!" .
( $message
? "<br>Additional information:<br>" . $message
: "<br>No additional details were provided. Sorry about that."
);
}

10 changes: 9 additions & 1 deletion common/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ th {
border-bottom: 1px solid black;
}

.submit {
.xbutton {
background: none!important;
border: none;
padding: 0!important;
Expand All @@ -101,6 +101,14 @@ th {
cursor: pointer;
}

.xsubmit {
}

.xdelete {
text: red;
background-color: red;
}

.success {
color: white;
background-color: var(--color5);
Expand Down
14 changes: 6 additions & 8 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,16 @@
$statement = $connection->prepare($sql);
$statement->execute();
$result = $statement->fetchAll();
} catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}

$sql = "SELECT 'Total users', COUNT(*) as count FROM users WHERE deleted = '0000-00-00 00:00:00'
UNION SELECT 'Total tools', COUNT(*) as count FROM tools WHERE deleted = '0000-00-00 00:00:00' AND offered=1
UNION SELECT 'Total loans', COUNT(*) as count FROM loans WHERE deleted = '0000-00-00 00:00:00'
UNION SELECT 'Total categories', COUNT(*) as count FROM taxonomy WHERE deleted = '0000-00-00 00:00:00'
";
$sql = "SELECT 'Total users', COUNT(*) as count FROM users WHERE deleted = '0000-00-00 00:00:00'
UNION SELECT 'Total tools', COUNT(*) as count FROM tools WHERE deleted = '0000-00-00 00:00:00' AND offered=1
UNION SELECT 'Total loans', COUNT(*) as count FROM loans WHERE deleted = '0000-00-00 00:00:00'
UNION SELECT 'Total categories', COUNT(*) as count FROM taxonomy WHERE deleted = '0000-00-00 00:00:00'
";
$statement = $connection->prepare($sql);
$statement->execute();
$result = $statement->fetchAll();
} catch(PDOException $error) { showMessage( __LINE__ , __FILE__ , $sql . "<br>" . $error->getMessage()); }
?>
<table>
<tr><th colspan=2 align="center">Statistics</th></tr>
Expand Down
4 changes: 2 additions & 2 deletions loans/edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
<blockquote class="success">Successfully updated the loan in the <a href="list.php">loan list</a>.</blockquote>
<?php endif; ?>

<form method="post"><input class="submit" type="submit" name="submit" value="Submit">
<form method="post"><input class="button submit" type="submit" name="submit" value="Submit">
<input type="hidden" name="csrf" value="<?php echo escape($_SESSION['csrf']); ?>">
<input type="hidden" name="id" value="<?php echo escape($loan['id']); ?>">
<input type="hidden" name="tool" id="tool" value="<?php echo escape($loan["tool"]); ?>"></label>
Expand All @@ -89,7 +89,7 @@
<label class="label" for="actualstart">Actual start<input class="input" type="text" name="actualstart" id="actualstart" value="<?php echo escape($loan["actualstart"]); ?>" ></label>
<label class="label" for="actualend">Actual end<input class="input" type="text" name="actualend" id="actualend" value="<?php echo escape($loan["actualend"]); ?>" ></label>

<input class="submit" type="submit" name="submit" value="Submit">
<input class="button submit" type="submit" name="submit" value="Submit">
</form>

<?php require "../common/footer.php"; ?>
2 changes: 1 addition & 1 deletion loans/list.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
<tbody>
<?php foreach ($result as $row) : ?>
<tr>
<td><a href="edit.php?id=<?php echo escape($row["id"]); ?>">Edit</a>&nbsp;<button class="submit" type="submit" name="submit" value="<?php echo escape($row["id"]); ?>">Delete!</button></td>
<td><a href="edit.php?id=<?php echo escape($row["id"]); ?>">Edit</a>&nbsp;<button class=" button submit" type="submit" name="submit" value="<?php echo escape($row["id"]); ?>">Delete!</button></td>
<td><?php echo ( escape($row["active"]) ? "active" : "-" ); ?></td>
<td><?php echo escape($row["toolname"]); ?></td>
<td><?php echo escape($row["username1"]); ?></td>
Expand Down
4 changes: 2 additions & 2 deletions loans/new.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<blockquote class="success">Successfully added a loan to the <a href="list.php">loans list</a>.</blockquote>
<?php endif; ?>

<form method="post"><input class="submit" type="submit" name="submit" value="Submit">
<form method="post"><input class="button submit" type="submit" name="submit" value="Submit">
<input type="hidden" name="csrf" value="<?php echo escape($_SESSION['csrf']); ?>">

<label class="label" for="active"><input class="input" type="checkbox" name="active" id="active" value=1 checked>active</label>
Expand All @@ -79,7 +79,7 @@
<label class="label" for="actualstart">actualstart<input class="input" type="text" name="actualstart" id="actualstart"></label>
<label class="label" for="actualend" >actualend <input class="input" type="text" name="actualend" id="actualend" ></label>

<input class="submit" type="submit" name="submit" value="Submit">
<input class="button submit" type="submit" name="submit" value="Submit">
</form>

<?php require "../common/footer.php"; ?>
4 changes: 2 additions & 2 deletions taxonomy/edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
<blockquote class="success">Successfully updated <b><?php echo escape($_POST['name']); ?></b> in the <a href="list.php">taxonomy list</a>.</blockquote>
<?php endif; ?>

<form method="post"><input class="submit" type="submit" name="submit" value="Submit">
<form method="post"><input class="button submit" type="submit" name="submit" value="Submit">
<input type="hidden" name="csrf" value="<?php echo escape($_SESSION['csrf']); ?>">
<input type="hidden" name="id" value="<?php echo escape($tax['id']); ?>">

Expand All @@ -80,7 +80,7 @@
</select>
</label>

<input class="submit" type="submit" name="submit" value="Submit">
<input class="button submit" type="submit" name="submit" value="Submit">
</form>

<?php require "../common/footer.php"; ?>
2 changes: 1 addition & 1 deletion taxonomy/list.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
<tbody>
<?php foreach ($result as $row) : ?>
<tr>
<td><a href="edit.php?id=<?php echo escape($row["id"]); ?>" class="submit">Edit</a>&nbsp;<button class="submit" type="submit" name="submit" value="<?php echo escape($row["id"]); ?>">Delete!</button></td>
<td><a href="edit.php?id=<?php echo escape($row["id"]); ?>" class="submit">Edit</a>&nbsp;<button class=" button submit" type="submit" name="submit" value="<?php echo escape($row["id"]); ?>">Delete!</button></td>
<td><?php echo escape($row["name"]); ?></td>
<td><?php echo escape($row["parentname"]); ?></td>
</tr>
Expand Down
4 changes: 2 additions & 2 deletions taxonomy/new.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<blockquote>Successfully added <b><?php echo escape($_POST['name']); ?></b> to the <a href="list.php">taxonomy list</a>.</blockquote>
<?php endif; ?>

<form method="post"><input class="submit" type="submit" name="submit" value="Submit">
<form method="post"><input class="button submit" type="submit" name="submit" value="Submit">
<input type="hidden" name="csrf" value="<?php echo escape($_SESSION['csrf']); ?>">

<label class="label" for="name">Name<input type="text" name="name" id="name"></label>
Expand All @@ -55,7 +55,7 @@
</select>
</label>

<input class="submit" type="submit" name="submit" value="Submit">
<input class="button submit" type="submit" name="submit" value="Submit">
</form>

<?php require "../common/footer.php"; ?>
6 changes: 2 additions & 4 deletions tools/detail.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
$statement->execute();

$result = $statement->fetchAll();
} catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}
} catch(PDOException $error) { showMessage( __LINE__ , __FILE__ , $sql . "<br>" . $error->getMessage()); }
}
?>
<?php require "templates/header.php"; ?>
Expand Down Expand Up @@ -96,7 +94,7 @@
<input name="csrf" type="hidden" value="<?php echo escape($_SESSION['csrf']); ?>">
<label for="owner">owner</label>
<input type="text" id="owner" name="owner">
<input class="submit" type="submit" name="submit" value="View Results">
<input class="button submit" type="submit" name="submit" value="View Results">
</form>

<?php require "../common/footer.php"; ?>
17 changes: 9 additions & 8 deletions tools/edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
WHERE id = :id';
$statement = $connection->prepare($sql);
$statement->execute($record);
} catch(PDOException $error) { echo $sql . "<br>" . $error->getMessage(); }
} catch(PDOException $error) { showMessage( __LINE__ , __FILE__ , $sql . "<br>" . $error->getMessage()); }
}

if (isset($_GET['id'])) { // Action on LOAD:
Expand Down Expand Up @@ -81,11 +81,8 @@
$statement->execute();
$tax = $statement->fetchAll();

} catch(PDOException $error) { echo $sql . "<br>" . $error->getMessage(); }
} else {
echo "Something went wrong!";
exit;
}
} catch(PDOException $error) { showMessage( __LINE__ , __FILE__ , $sql . "<br>" . $error->getMessage()); }
} else { showMessage( __LINE__ , __FILE__ ); exit; }
?>

<h2>Edit a tool</h2>
Expand All @@ -94,7 +91,11 @@
<blockquote class="success">Successfully updated your <b><?php echo escape($_POST['toolname']); ?></b> in the <a href="list.php">tool pool</a>.</blockquote>
<?php endif; ?>

<form method="post"><input class="submit" type="submit" name="submit" value="Submit">
<form method="post">
<button class="button delete" type="submit" name="delete" value="<?php echo escape($tool["id"]); ?>" action="list.php">Delete!</button>
</form>

<form method="post"><input class="button submit" type="submit" name="submit" value="Submit">
<input type="hidden" name="csrf" value="<?php echo escape($_SESSION['csrf']); ?>">
<input type="hidden" name="id" value="<?php echo escape($tool['id']); ?>">

Expand Down Expand Up @@ -177,7 +178,7 @@
<label class="label" for="hydraulic"> <input class="input" type="checkbox" name="hydraulic" id="hydraulic" value="1" <?php echo ( escape($tool["hydraulic"] ) ? "checked" : NULL ) ?>>Hydraulic</label>
<label class="label" for="pneumatic"> <input class="input" type="checkbox" name="pneumatic" id="pneumatic" value="1" <?php echo ( escape($tool["pneumatic"] ) ? "checked" : NULL ) ?>>Pneumatic</label>

<input class="submit" type="submit" name="submit" value="Submit">
<input class="button submit" type="submit" name="submit" value="Submit">
</form>

<?php require "../common/footer.php"; ?>
10 changes: 5 additions & 5 deletions tools/list.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
$statement = $connection->prepare($sql);
$statement->execute($record);
//var_dump($statement);
} catch(PDOException $error) { echo $sql . "<br>" . $error->getMessage(); }
} catch(PDOException $error) { showMessage( __LINE__ , __FILE__ , $sql . "<br>" . $error->getMessage()); }
}

if (isset($_POST['delete'])) { // Action on SUBMIT:
Expand All @@ -56,7 +56,7 @@
$statement->bindValue(':id', $id);
$statement->execute();
$success = "Successfully deleted the tool.";
} catch(PDOException $error) { echo $sql . "<br>" . $error->getMessage(); }
} catch(PDOException $error) { showMessage( __LINE__ , __FILE__ , $sql . "<br>" . $error->getMessage()); }
}

// Action on LOAD:
Expand Down Expand Up @@ -86,7 +86,7 @@
$statement->execute();
$tax = $statement->fetchAll();

} catch(PDOException $error) { echo $sql . "<br>" . $error->getMessage(); }
} catch(PDOException $error) { showMessage( __LINE__ , __FILE__ , $sql . "<br>" . $error->getMessage()); }
?>

<h2>Tool Pool || <a href="new.php">add new</a></h2>
Expand Down Expand Up @@ -125,9 +125,9 @@
<?php foreach ($result as $row) : ?>
<tr>
<td>
<button class="submit" type="submit" name="loan" value="<?php echo escape($row["id"]); ?>">Loan</button>
<button class="button edit" type="submit" name="loan" value="<?php echo escape($row["id"]); ?>">Loan</button>
<a href="edit.php?id=<?php echo escape($row["id"]); ?>">Edit</a>
<button class="submit" type="submit" name="delete" value="<?php echo escape($row["id"]); ?>">Delete!</button>
<button class="button delete" type="submit" name="delete" value="<?php echo escape($row["id"]); ?> action="list.php">Delete!</button>
</td>
<td><?php echo escape($row["username"]); ?></td>
<td
Expand Down
10 changes: 5 additions & 5 deletions tools/new.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
);
$statement = $connection->prepare($sql);
$statement->execute($record);
} catch(PDOException $error) { echo $sql . "<br>" . $error->getMessage(); }
} catch(PDOException $error) { showMessage( __LINE__ , __FILE__ , $sql . "<br>" . $error->getMessage()); }
}

// Action on LOAD:
Expand All @@ -59,8 +59,8 @@
$statement = $connection->prepare($sql);
$statement->execute();
$tax1 = $statement->fetchAll();
} catch(PDOException $error) { echo $sql . "<br>" . $error->getMessage(); }
} catch(PDOException $error) { echo $sql . "<br>" . $error->getMessage(); }
} catch(PDOException $error) { showMessage( __LINE__ , __FILE__ , $sql . "<br>" . $error->getMessage()); }
} catch(PDOException $error) { showMessage( __LINE__ , __FILE__ , $sql . "<br>" . $error->getMessage()); }

//var_dump($tax1);
?>
Expand All @@ -71,7 +71,7 @@
<blockquote class="success">Successfully added <b><?php echo escape($_POST['toolname']); ?></b> to the <a href="list.php">tool pool</a>.</blockquote>
<?php endif; ?>

<form method="post"><input class="submit" type="submit" name="submit" value="Submit">
<form method="post"><input class="button submit" type="submit" name="submit" value="Submit">
<input type="hidden" name="csrf" value="<?php echo escape($_SESSION['csrf']); ?>">

<label class="label" for="owner">Owner
Expand Down Expand Up @@ -104,7 +104,7 @@
<label class="label" for="hydraulic"><input class="input" type="checkbox" name="hydraulic" id="hydraulic" value=1>Hydraulic</label>
<label class="label" for="pneumatic"><input class="input" type="checkbox" name="pneumatic" id="pneumatic" value=1>Pneumatic</label>

<input class="submit" type="submit" name="submit" value="Submit">
<input class="button submit" type="submit" name="submit" value="Submit">
</form>

<?php require "../common/footer.php"; ?>
Loading

0 comments on commit cdb9485

Please sign in to comment.