How would you handle the error being returned to figure out which field in your form is wrong?
Main Topics
Browse All TopicsHi,
I'm inserting records into a mysql database through a form in php. Is it possible to catch an error thrown by the msql database and redisplay the form that is being submitted so the entered values are not lost? I know I can check the values with javascript before the form is submitted, but I was wondering if this can be done with php and try catch blocks??
I'm using mysql 5.0.16 and php 5.1.1
Thanks for your time.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
When a mySQL query fails, it would be very near impossible to associate an error message to a specific field in your form. Before your data reaches mySQL, you should validate each field to ensure it has an acceptable value. And you should do this through PHP (or other serverside technology) rather than javascript. (Javascript validation can still be helpful in addition to server-side validation, but shouldn't be relied upon exclusively).
See below for a quick example of validation using PHP:
<?php
//validation
if( isset($_POST['submit']) ) { //only validate if we have submitted
if( !strlen($_POST['variable1'
$error['variable1'] = "Variable 1 must be set!";
if( !strlen($_POST['variable2'
$error['variable2'] = "Variable 2 must be set!";
else if( !is_numeric($_POST['variab
$error['variable2'] = "Variable 2 must be a number!";
}
if( !isset($error) ) { //if no errors
//then do your database stuff
}
//display form
?>
<form method="post" action="<?= $_SERVER['PHP_SELF'] ?>">
var1: <input type="text" name="variable1" value="<?php if( isset($_POST['variable1'])
var2: <input type="text" name="variable2" value="<?php if( isset($_POST['variable2'])
<input type="submit" name="submit" value="Submit" />
</form>
<?php
if( isset($_POST['submit']) )
if(!isset($error) )
echo "<b>Form OK!</b>\n";
else
echo "<b>Form has " . sizeof($error) . " error(s).\n</b>";
?>
//end code
Just an example. The above checks that variable1 has a value and that variable2 is a number. If either is not the case, then it outputs an error message to the right of the field... You would likely want to standardize your own functions for error checking, which would allow you to rewrite the above more efficiently with standardized error messages.
The point to take home from all this is that you yourself must understand what values are acceptable for the underlying database.
Business Accounts
Answer for Membership
by: mensuckPosted on 2006-01-10 at 12:30:41ID: 15664699
database errors are not * exceptions * so try{}, catch(exception $e){} will not work in this case! But you can use
mysql_query ('...query...') or other_function_call(); instead of die();
Or you can just look for the error returned, and do what you need to do based on return error code being found in the error mysql_error () outputs!
ms!