Jump to content

[SOLVED] Error reporting not working


clang

Recommended Posts

I am trying to set up an error/message reporting system. Something real simple. I'll post code in a second. Right now the code is working most of the time, but sometimes it just stops working and I can't figure out why.

 

First the reporting code.

<?php
function setError($errorNum)
{
if(isset($_SESSION['error']))
{
	unset($_SESSION['error']);
}

$_SESSION['error']=$errorNum;
}
?>

This is the function which sets the error. A simple 3 digit number is passed to this function (I'll show an example of that in a bit). This number is the index of the message in the MySQL database. More is explain in the next function.

 

<?php
function checkError($db)
{
if(isset($_SESSION['error']))
{
	$query= "SELECT * FROM `errors` WHERE `errorNum`= ".$_SESSION['error']."";
	$result= $db->query($query) or die('Error, query failed'. mysqli_error($db));
	$row = $result->fetch_assoc();
	echo "<div class=\"".$row['errorType']."\"><p align=\"center\">".$row['errorMessage']."</p></div>";
	unset($_SESSION['error']);
}
}
?>

This is the function which reports the message. Using the number stored, it will find the correct row in the database, and grab the message and color scheme. Once it's done it will unset the session variable so that the same message doesn't pop up over and over.

 

Both functions are linked to in all files they are used by an include statement to my library of functions.

 

Now for examples of usage.

 

First a page where some problems are occurring. This page does not set any errors, only reports them.

<?php
include 'library/functions.php';

$db=opendb($dbhost, $dbuser, $dbpass, $dbname);
session_start(); 
HTMLheader($db,"Companies",'10','Yes',"Default");

?>
<div class="title"><a class="likeParent" href="./Search.php">Companies List</a></div>
<?php 
checkError($db)
?>
<h2><a href="./CreateCompany.php">Click here to create a new company.</a></h2><br />
<h2> List of Companies in the system</h2>
<?php
$result=listCompanies($db);
$x=1;
print ("<table>\n
	<tr><th>Company Name</th><th>Edit Company</th><th>List Branches</th><th>Delete Company</th></tr>\n");
for($a=0;$a<$result->num_rows;$a++)
{
	$row = $result->fetch_assoc();
	echo "<tr><td>".stripslashes($row['Name'])."</td><td>
			<a href=\"./EditCompany.php?abbreviation=".stripslashes($row['Abbreviation'])."\">Edit</a></td>\n
			<td>
			<a href=\"./ListBranches.php?abbreviation=".stripslashes($row['Abbreviation'])."\">List Branches</a></td>\n
			<td><a href=\"./DeleteCompany.php?abbreviation=".$row['Abbreviation']."\">Delete</a></td>\n";
}
echo"</table>\n";

HTMLfooter();
?>

 

This next page is able to correctly set messages. When a company is correctly entered into the system is sets the message 106, which is "Company was succefully created" and transfers to the ListCompanies page (show above). Works great all the time.

<?php
include 'library/functions.php';

$db=opendb($dbhost, $dbuser, $dbpass, $dbname);
session_start(); 
HTMLheader($db,"Create Company",'10', 'Yes',"Default");

$result = getCountyInfo($db);
$count= $result->num_rows;

if((isset($_POST['companyName'])))
{
$companyName=$_POST['companyName'];

insertCompany($db,$companyName);

setError(106);
header("Location: ./ListCompanies.php", true, 302 );
}
else
{
print("<div class=\"title\"><a class=\"likeParent\" href=\"./Search.php\">Create a New Company</a></div>");
print("<form action=\"./CreateCompany.php\" method=\"POST\">\n
	<table>\n
	<tr><td><LABEL for=\"companyName\">Company Name: </label></td>
              <td><INPUT type=\"text\" name=\"companyName\"></td></tr>\n
	</table>\n");

echo"<INPUT type=\"submit\" value=\"Submit\"> <INPUT type=\"reset\" Value=\"Clear Form\">\n";
echo"</form>\n";

}
HTMLfooter();
?>

 

This final page is an example of one that doesn't work. It sets an message in the same way, and transfers to ListCompanies in the same way, but nothing show up once you get to ListCompanies.

<?php
include 'library/functions.php';

$db=opendb($dbhost, $dbuser, $dbpass, $dbname);
session_start(); 
HTMLheader($db,"Delete Company",'10','Yes',"Default");

if(isset($_GET['abbreviation']))
{
$abbreviation=$_GET['abbreviation'];
if(isset($_POST['Yes']))
{
	deleteCompany($db,$abbreviation);
	setError(108);
	header( 'Location: ./ListCompanies.php', true, 302 );
}
else if(isset($_POST['No']))
{
	setError(108);
	header("Location: ./ListCompanies.php", true, 302 );
}
}
else
{
header( 'Location: ./ListCompanies.php', true, 302 );
}

?>

<div class="title"><a class="likeParent" href="./Search.php">Delete Company <?php echo $abbreviation;?></a></div>
<div id="content">

<?php
checkError($db);
?>
<form action="./DeleteCompany.php?abbreviation=<?php echo $abbreviation;?>" method="post" name="form">

<table>
<tr>
<td class="text" colspan="2">
<div class="errorYellow">
Are you sure you would like the delete the company <?php echo $abbreviation;?>?
</div>
</td>
</tr>
<tr>
<td align="center">
<input type="submit" name="Yes" value="Yes" >
</td>
<td align="center">
<input type="submit" name="No" value="No" >
</td>
</tr>
</table>
</form>
</div>
<script type="text/javascript">
document.form.documentbranchName0.focus();
</script>
<?php
HTMLfooter();
?>

 

These are just one set of examples. This problem is occurring on a select number of other pages as well, although for the most part, the system works well.

If you need anything explained better let me know. Any help is really appreciated.

Link to comment
Share on other sites

The first set of code I posted is the method I have which sets the message. The second set of code is the method which displays the message.

It knows which message to display by the number which is saved in a Session variable. The problem is some times it works, and some times it doesn't.

Link to comment
Share on other sites

<?php
function setError($errorNum)
{
if(isset($_SESSION['error']))
{
	unset($_SESSION['error']);
}

$_SESSION['error']=$errorNum;
}
?>

 

In this line of code its saying if its set then unset the variable since its unset it cant be redelared try:

<?php
function setError($errorNum)
{
if(isset($_SESSION['error']))
{
                          $_SESSION['error']=$errorNum;
	unset($_SESSION['error']);
}
$_SESSION['error']=$errorNum;
}
?>

Link to comment
Share on other sites

What would be the point of setting it, unsetting it, then setting it again?

And I just tried with this code, and still had the same problem.

 

<?php
function setError($errorNum)
{
if(isset($_SESSION['error']))
{
	$_SESSION['error']=$errorNum;
}
else
{
	$_SESSION['error']=$errorNum;
}
}
?>

Link to comment
Share on other sites

Alright I figured it out.

For any body that ever needs it the problem was that the error was getting erased before it ever made it to the new page.

I assumed php saw a header() command and just stopped processing the rest of the page. It doesn't.

Instead I continued to process the rest of the page like normal.

When it made it to the checkError() function it displayed the error, and reset the Session value back to null. The problem is we never see the page it was displayed on because we're sent to the new page.

 

That's why create company worked and delete company didn't, because create company didn't have checkError() in it.

The fix I made was just a boolean value up top, initialized to true. When I set an error before a header, I change it to false. Then I check the boolean before I do checkError(). So that page won't process checkError() if I've got the boolean set to false.

 

I'd post code, but I doubt any one cares.

Glad it's fixed.

Oh and I change unset($_SESSION['error']) to $_SESSION['error']=null to keep things sexier.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.