footer problem when query dies

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
synical21
Forum Contributor
Posts: 150
Joined: Tue Jul 28, 2009 8:44 am
Location: London UK

footer problem when query dies

Post by synical21 »

Hello code experts have not bumped into a problem in a while using php but im back again :mrgreen:

Ill show you the piece of code which is causing me trouble:

Code: Select all

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
 
$total_cost = round(($_POST['amountworkers'] * $_POST['perperson']) * 1.05, 2);
$user_cost = $_SESSION['user_money'];
$userid = $_SESSION['user_id'];
 
if($total_cost > $user_cost)
{
    die("You have insufficient funds to create this job, please allow room for a 5% charge onto your job total cost.");
}
else{
    $_SESSION['user_money'] = round($user_cost - $total_cost, 2);
Baisically when the code dies it obviously does not continue reading the rest of the page as no footer is displayed, how can i over come this? do i need to echo the html after the error message? Just some guidence please.

If it helps this is the footer code which is missed:

Code: Select all

</div>
            <br class="clearFloat" />
        </div>
    </div>
</div>
<div style="width: 890px; background: #fff; margin:  auto; padding: 0; position: relative;"> 
<div id="outerWrapper">
<div id="footer">
    <p><a href="#">Home</a>| <a href="#">Services</a> | <a href="#">About Us</a> | <a href="#">Contact Us</a> | <a href="#">Terms & Conditons</a> | <a href="#">Privacy</a></p>
    <p>This site is copyright © 2009 </p>
  
</div>
</div>
</div>
 
</body>
</html>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: footer problem when query dies

Post by requinix »

You're talking about line 11?

If you don't want it to die, don't make it die. That simple. Try echo instead.
synical21
Forum Contributor
Posts: 150
Joined: Tue Jul 28, 2009 8:44 am
Location: London UK

Re: footer problem when query dies

Post by synical21 »

Ooops i dont know if that works because i dont want it to continue the rest of the script, and it has when i use echo

here is the whole the script

Code: Select all

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
 
$total_cost = round(($_POST['amountworkers'] * $_POST['perperson']) * 1.05, 2);
$user_cost = $_SESSION['user_money'];
$userid = $_SESSION['user_id'];
 
if($total_cost > $user_cost)
{
   echo ("You have insufficient funds to create this job, please allow room for a 5% charge onto your job total cost.");
}
else{
    $_SESSION['user_money'] = round($user_cost - $total_cost, 2);
 
$sql = "SELECT `fulldata`.*
   FROM `fulldata`
  WHERE fulldata.createuser_id = $userid";
 $result = mysql_query($sql)
     or die('Invalid query: ' . $sql . ' - Error is ' . mysql_error());
     
      $sql = "UPDATE `users`
    SET users.user_money = round($user_cost - $total_cost, 2)
  WHERE users.id = $userid";
 $result = mysql_query($sql)
     or die('Invalid query: ' . $sql . ' - Error is ' . mysql_error());
}
 
 
 
 
 
//Function definition
function onlyLetters($str){
   $text = str_replace("\n", "xyxy", $str);
   $pattern = '/[^0-9a-zA-Z-. ]*/';
   $text = preg_replace($pattern, '', $text);
   return str_replace("xyxy", "<br />\n", $text);
}
function onlyNumbers($str){
        $pattern = '/[^0-9.]*/';
        return preg_replace($pattern, '', $str);
}
 
 
// Get and strip values  from form 
$title=onlyLetters($_POST['title']);
$descript=onlyLetters($_POST['descript']);
$proof=onlyLetters($_POST['proof']);
$min=onlyNumbers($_POST['min']);
$amountworkers=onlyNumbers($_POST['amountworkers']);
$perperson=onlyNumbers($_POST['perperson']);
$createuser_id=onlyNumbers($_POST['createuser_id']);
$user_name=onlyLetters($_POST['user_name']);
 
// Insert data into mysql 
$sql="INSERT INTO $tbl_name(title, descript, proof, min, amountworkers, perperson, createuser_id, user_name)VALUES('$title', '$descript', '$proof', '$min', '$amountworkers', '$perperson', '$createuser_id', '$user_name')";
$result=mysql_query($sql);
 
 
// if successfully insert data into database, displays message "Successful". 
if($result){
echo "Thank you for submiting your job, our team will now take a look and approve very soon ";
echo "<BR>";
echo "<a href='http://miniworkers.justfree.com/jobs.php'>Click here to go back to Minute Workers</a>";
}
 
else {
echo "ERROR";
}
 
      
?>
</h1>
<p>
  <br />
</p>
  </div>
            <br class="clearFloat" />
        </div>
    </div>
</div>
<div style="width: 890px; background: #fff; margin:  auto; padding: 0; position: relative;"> 
<div id="outerWrapper">
<div id="footer">
    <p><a href="#">Home</a>| <a href="#">Services</a> | <a href="#">About Us</a> | <a href="#">Contact Us</a> | <a href="#">Terms & Conditons</a> | <a href="#">Privacy</a></p>
    <p>This site is copyright © 2009</p>
  
</div>
</div>
</div>
 
</body>
</html>
when i took out the die and used echo this part of the whole script still works which means inserting data into the db what shouldnt be.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: footer problem when query dies

Post by jackpf »

Use an error handler, and trigger_error(). That way you can redirect to an error page, or load an error template, or whatever.

Like this: http://jackpf.co.uk/index.php?action=forum&forum=20349
That forum doesn't actually exist, so you should get an error, yet the footer still displays :D All I do is load the error template, display it, and then stop the script parsing.
synical21
Forum Contributor
Posts: 150
Joined: Tue Jul 28, 2009 8:44 am
Location: London UK

Re: footer problem when query dies

Post by synical21 »

Sounds good ill google it now :D
synical21
Forum Contributor
Posts: 150
Joined: Tue Jul 28, 2009 8:44 am
Location: London UK

Re: footer problem when query dies

Post by synical21 »

Ok i looked at a tutorial and understand the basics of it, this is what ive done so far

Code: Select all

<?php
//error handler function
function customError($errno, $errstr)
  {
  echo "<b>Error:</b> [$errno] $errstr<br />";
  echo "Ending Script";
  die();
  }
 
//set error handler
set_error_handler("customError",E_USER_ERROR);
 
//trigger error
$total_cost = round(($_POST['amountworkers'] * $_POST['perperson']) * 1.05, 2);
$user_cost = $_SESSION['user_money'];
$userid = $_SESSION['user_id'];
 
if($total_cost > $user_cost)
{
   trigger_error("You have insufficient funds to create this job, please allow room for a 5% charge onto your job total cost.",E_USER_ERROR);
}
?>
Just got a question about the error handler though, i have not done the //error handler function. Would i just add a redirection link there which leads to error.php then create the error page? Or is there a different way? Just a bit confused how to link to an error handler probably overlooking something simple
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: footer problem when query dies

Post by superdezign »

I think that exceptions would be better suited for this issue. Error handling is more PHP's job than the developer's. Exceptions are our turf.
synical21
Forum Contributor
Posts: 150
Joined: Tue Jul 28, 2009 8:44 am
Location: London UK

Re: footer problem when query dies

Post by synical21 »

Ok ill read up on it now
synical21
Forum Contributor
Posts: 150
Joined: Tue Jul 28, 2009 8:44 am
Location: London UK

Re: footer problem when query dies

Post by synical21 »

Ok ill read up on it now

EDIT:

Hmm this is more confusing for me, i have tried breifly to make an exception but im getting confused:

Code: Select all

<?php
$total_cost = round(($_POST['amountworkers'] * $_POST['perperson']) * 1.05, 2);
$user_cost = $_SESSION['user_money'];
$userid = $_SESSION['user_id'];
 
//create function with an exception
function checkcosts($totalcost,$user_cost)
  {
  if($total_cost > $user_cost)
    {
    throw new Exception("You have not enough money etc etc...");
    }
  return true;
  }
//trigger exception in a "try" block
try
  {
  checkcosts($total_cost < $user_cost);
  //If the exception is thrown, this text will not be shown
  echo 'urm dunno why i need this but its in the example maybe insert rest of script here what updates the database?';
  }
  //catch exception
catch(Exception $e)
  {
  echo 'Message: ' .$e->getMessage();
  }
?>
Thats all i done, i cant understand what it means by "catch" i have read the tutorial w3schools but im still confused on it.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: footer problem when query dies

Post by superdezign »

Basically, catch catches anything thrown by throw. You tell it what kind of things to catch, and you can have multiple catch statements to catch different types of things.

Code: Select all

try {
    if ($somethingFails) {
        throw new Exception("error message");
    }
    
    // Continue business logic
} catch (Exception $e) {
    // There has been an error
}
 
// Output footer after try/catch block
Anything in a try block after the exception has been thrown will not execute. Instead, after the exception is thrown, the program will jump straight to the appropriate catch block and continue from there.
synical21
Forum Contributor
Posts: 150
Joined: Tue Jul 28, 2009 8:44 am
Location: London UK

Re: footer problem when query dies

Post by synical21 »

That clears it up thank you

EDIT: Now working perfectly with your example thanks again
Post Reply