Displaying MySQL errors on the same page with php.

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
vorathe
Forum Newbie
Posts: 1
Joined: Tue Apr 27, 2010 10:51 am

Displaying MySQL errors on the same page with php.

Post 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!
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

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

Post 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.
davex
Forum Contributor
Posts: 101
Joined: Sat Feb 27, 2010 4:10 pm
Location: Namibia

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

Post 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.
Post Reply