Page 1 of 1

Displaying MySQL errors on the same page with php.

Posted: Tue Apr 27, 2010 11:11 am
by vorathe
Hi,

I have a page which in essence is used register new users. It contains multiple form input type fields, such as first name, last name, and account id.

Currently I'm using php to check if the account id in the form has a duplicate in the database, and if so to return a error message. This is working, however the mysql error is being displayed on a blank white page on submit, because I haven't specified for it to do anything else.

I'd like to have the mysql duplicate key error display on the same page as the form like a validation message.

This is how I display my error:

Code: Select all

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT  INTO tblcustomeraccount (Points, AccountId) VALUES (%s, %s)",
                       GetSQLValueString($_POST['Points'], "int"),
                       GetSQLValueString($_POST['AccountId'], "int"));

  mysql_select_db($database_connDB, $connDB);
  $Result1 = mysql_query($insertSQL, $connDB) or die('<b>An error occurred because:</b> ' . mysql_error());
I'm still very new to all of this, so any insight would be greatly appreciated.

Thanks!

Re: Displaying MySQL errors on the same page with php.

Posted: Tue Apr 27, 2010 11:28 am
by a94060
If you just take out the die portion of your code, If there is an error,it will appear before the rest of your code runs. However, im not sure if it actually displays anything personal.

Re: Displaying MySQL errors on the same page with php.

Posted: Tue Apr 27, 2010 11:30 am
by davex
Hi,

You probably want to look at mysql_errno and mysql_error.

Use would be as follows:

Code: Select all

if (!mysql_query($my_query,$sql))
 {
 $error = mysql_errno($sql).": ".mysql_error($sql); // error has occured and details in this string
 }
else
 {
 // all went well
 }
Cheers,

Dave.