/    Sign up×
Community /Pin to ProfileBookmark

how to display custom error page

Morning guys, I am trying to display a custom error page called “article_submission_error.php” if the article is not submitted to the database. I don’t know how to display the page if there is an error. Perhaps we could use the code below but I don’t know what the $error variable would be or how to trip that. Below you will see my query and the html page that I am using.

PS: i dont have any PDO extensions just regular php

if($error) {
include ‘path/to/error.php’;
exit;

[code]

function newArticle($db, $userID, $articleTitle, $articleSummary, $articleContent, $publicationDate)
{
$query = ‘insert into Articles
(
publicationDate,
articleTitle,
articleSummary,
articleContent,
userID)
VALUES
(
:publicationDate,
:articleTitle,
:articleSummary,
:articleContent,
:userID
)’;
[/code]

Html Page with fields

[code]
<!DOCTYPE html>
<html lang=”en” xmlns=”http://www.w3.org/1999/html”>
<HEAD>
<meta charset=”UTF-8″>
<title><?php include (‘title.php’) ?></title>
</HEAD>
<BODY>
<?php include (‘sections/menubar.php’) ?>
<?php //include( ‘sections/searchBar.php’ );

<section id=”addParagraphSection”>

<header><h1>Add New Article </h1></header>

<form action=”addNewArticles.php”<?php //”sections/queries.php”*/?> method=”post” id=”addNewArticle”>
<table>
<tr>
<td><label>Article Title:</label></td>
<td><input type=”text” name=”articleTitle” required></td>
</tr>

<tr>
<td><label>Article Summary:</label></td>
<td><textarea rows=”3″ cols=”70″ name=”articleSummary” ></textarea></td>
</tr>

<tr>
<td><label>Article Content:</label></td>
<td><textarea rows=”22″ cols=”70″ name=”articleContent” ></textarea></td>
</tr>

<tr>
<td><label>Start Publishing Date:</label></td>
<td><input type=”date” name=”publicationDate” required value=”<?php echo date(‘Y-m-d’); ?>”></td>
</tr>

</table>

<input type =”hidden” name=”newArticle” value=”new”>

<input type =”hidden” name=”ID” value=”<?php echo $ID ?>”>
<input type =”hidden” name=”user” value=”<?php echo $_SESSION[‘user’]?>”>

<input type=”submit” name=”submit” value=”submit”>
<button type =”reset” value=”reset”>reset</button>
<button type=”button” onclick=”history.go(-1);”>Back </button>

</Form>

</section>
<footer>
<?php include( “sections/footer.php” ); ?>
</footer>
</BODY>
</HTML>
[/code]

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJan 21.2017 — I don't see where in your posted code you actually execute the DB query (or call that newArticle() function, assuming at some point it actually executes the query)?
Copy linkTweet thisAlerts:
@PeaceTime2323authorJan 21.2017 — Wow! I don't know how I forgot that,lol. Here is the correct page

<i>
</i>function newArticle($db, $userID, $articleTitle, $articleSummary, $articleContent, $publicationDate)
{
$query = 'insert into Articles
(
publicationDate,
articleTitle,
articleSummary,
articleContent,
userID)
VALUES
(
:publicationDate,
:articleTitle,
:articleSummary,
:articleContent,
:userID
)';

<i> </i>$statement = $db-&gt;prepare($query);

<i> </i>$statement-&gt;bindValue( ':publicationDate' , $publicationDate );
<i> </i>$statement-&gt;bindValue( ':articleTitle' , $articleTitle );
<i> </i>$statement-&gt;bindValue( ':articleSummary' , $articleSummary );
<i> </i>$statement-&gt;bindValue( ':articleContent' , $articleContent );
<i> </i>$statement-&gt;bindValue( ':userID' , $userID );

<i> </i>$statement-&gt;execute();
<i> </i>$statement-&gt;closeCursor();
}

Copy linkTweet thisAlerts:
@evoluerJan 21.2017 — I am not sure of your code, but I think you need to use redirect

if($error) {

header("Location: article_submission_error.php");

die();

}
Copy linkTweet thisAlerts:
@ginerjmJan 21.2017 — I dont' see where you are doing any error checking (big mistake) so I don't see how you would expect to be able to trap them. Assuming that your question implies that you want to trap situations that arise when your record is not posted/updated, that would be the key. As said above checking for an error and grabbing the error message produced and sending that to an error page of your own design thru a redirect would be the answer.

PS - You state that you don't have any pdo extensions just regular php. What are you referring to there? What db interface are you planning to use here? The long-deprecated MySQL or maybe mysqlI ?
Copy linkTweet thisAlerts:
@PeaceTime2323authorJan 21.2017 — yes, i am a beginning programmer using mysql. If I put that code on top of all my pages it will check for errors? and then I can combine the below code some how?

if($error) {

header("Location: article_submission_error.php");

die();

}
Copy linkTweet thisAlerts:
@evoluerJan 21.2017 — Please post the full code including database connection strings used, only then will we be able to help you
Copy linkTweet thisAlerts:
@ginerjmJan 21.2017 — 1 - DO NOT use the MySQL interface. If you take the time to read the php manual you will see it is no longer supported so make the switch now. Are you sure you don't have PDO available to you? If not, as least go to mysqlI.

My signature shows how to turn on error checking to help you correct your code as you are writing it. It does not detect programming/logic errors or failures. Those you have to code for. Such as when you execute a query. Check the result to be sure it executed. Again - read the manual to see the syntax of the execute command and it may make more sense to you.

PS - once you are done with the development phase you should change that error checking code so as no to display errors to the user but rather log them to the error_log file. Again - check the manual.
Copy linkTweet thisAlerts:
@NogDogJan 21.2017 — That function is definitely using PDO (based on the bound parameter place-holders starting with ":" characters).

Wow! I don't know how I forgot that,lol. Here is the correct page

<i>
</i>function newArticle($db, $userID, $articleTitle, $articleSummary, $articleContent, $publicationDate)
{
$query = 'insert into Articles
(
publicationDate,
articleTitle,
articleSummary,
articleContent,
userID)
VALUES
(
:publicationDate,
:articleTitle,
:articleSummary,
:articleContent,
:userID
)';

<i> </i>$statement = $db-&gt;prepare($query);

<i> </i>$statement-&gt;bindValue( ':publicationDate' , $publicationDate );
<i> </i>$statement-&gt;bindValue( ':articleTitle' , $articleTitle );
<i> </i>$statement-&gt;bindValue( ':articleSummary' , $articleSummary );
<i> </i>$statement-&gt;bindValue( ':articleContent' , $articleContent );
<i> </i>$statement-&gt;bindValue( ':userID' , $userID );

<i> </i>$statement-&gt;execute();
<i> </i>$statement-&gt;closeCursor();
}
[/QUOTE]


That means you have the option of trapping all errors via exceptions, and then catching them.
[code=php]
$db->setAttribute(PDO::ERRMODE_EXCEPTION, true);
try {
// your code, including calls to that function
}
catch(PDOException, $e) {
// here's where you could load a database error page,
// or redirect to it, or whatever else when you get a DB error
}
catch(Exception, $e) {
// handle any other kind of exception here
}
[/code]
Copy linkTweet thisAlerts:
@PeaceTime2323authorJan 23.2017 — Thank you for all your feedback guys, I will take it and put it to good use.

I am off to change the world..
×

Success!

Help @PeaceTime2323 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...