views:

85

answers:

2

I am using following code. When the query crashes, it is not displaying the ALERT that I defined in the "catch" block.

<?php
    error_reporting(E_ALL ^ E_NOTICE);
    require_once("../Lib/dbaccess.php");

    //Retrieve values from Input Form
    $CategoryName = $_POST["inCategory"];
    $TotalMembers = $_POST["inTotalMembers"];
    $Details = $_POST["inDetails"];
    $CategoryName = $_POST["inCategory"];
    $Chairman = $_POST["inChairman"];

    $InsertQuery = "REPLACE INTO electioncategorymaster (ecname, ecdescription, ectotalmembers, ecchairman, lastupdated) VALUES ('".$CategoryName."','".$Details."',".$TotalMembers.",'".$Chairman."',now())";
    try
    {
        $Result = dbaccess::InsertRecord($InsertQuery); 
    }
    catch(exception $ex)
    {
        echo "<script type='text/javascript'>alert('".$ex."');</script>";
    }
?>
+6  A: 

If you want to get the message of the exception, you should use :

$ex->getMessage();

And not only $ex.

Also, you should escape the quotes in that string, to be sure to have some valid Javascript string -- addslashes might help, here.


If that doesn't change a thing :

  • are you sure there is an exception thrown ?
  • can you take a look at the output of your script ? ("view source" in your browser)


Also, if you want to get the full stack-trace of the exception, you might want to use something like this, instead of doing a JS alert :

echo '<pre>';
var_dump($ex);
echo '</pre>';

And, as always : installing the great Xdebug extension can help a lot, on a development server ;-)

Pascal MARTIN
+1  A: 

For future reference, when outputting values to JavaScript from PHP it's usually best just to use json_encode. This removes the need to encapsulate it in quotes and escape it.

Will Vousden