Jump to content


Photo

How to handle MySQL errors ?


  • Please log in to reply
3 replies to this topic

#1 adrianTNT

adrianTNT
  • Members
  • PipPipPip
  • Advanced Member
  • 146 posts

Posted 01 September 2006 - 05:30 PM

Hello.

I made a membership signup form. (I dont have many PHP+MySQL skills, this was made in Dreamweaver).
The user completes a form to signup (signup.php).
If the new user is successfully created then is sent to message.php telling him that the new username was created.
BUT
If the username entered is already in database then the singup.php page replaces all site layout content and posts the error
"Duplicate entry 'Adrian' for key 2".

How can I handle these errors so that if the username is already taken then the visitor is sent to
message.php?message=Username Taken
Rather than showing the ugly SQL error on page.

Thank you.
- Adrian.

I dont know if this helps, the code I have now on signup.php:


if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO affiliates (aff_full_name, aff_user_name, aff_email, aff_pass) VALUES (%s, %s, %s, %s)",
                       GetSQLValueString($_POST['full_name_box'], "text"),
                       GetSQLValueString($_POST['username_box'], "text"),
                       GetSQLValueString($_POST['email_box'], "text"),
                       GetSQLValueString($_POST['pass_box'], "text"));

  mysql_select_db($database_affiliates, $affiliates);
  $Result1 = mysql_query($insertSQL, $affiliates) or die(mysql_error());

  $insertGoTo = "message.php?message=New username created successfully.&type=info";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}




#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 01 September 2006 - 05:43 PM

Well, you have a die() statement -- just write a custom handler.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 adrianTNT

adrianTNT
  • Members
  • PipPipPip
  • Advanced Member
  • 146 posts

Posted 01 September 2006 - 06:55 PM

Well, you have a die() statement -- just write a custom handler.

Thanks Fenway, so I guess I have to replace
die(mysql_error());
with myfunction(); ?!
Another question in this case...
What php code do I need in order to open an URL?
So that I replace the "die" block with something like:
Open url : message.php?message=Error, user already exists.
How can I open the URL by php code?

Thank you.


#4 adrianTNT

adrianTNT
  • Members
  • PipPipPip
  • Advanced Member
  • 146 posts

Posted 01 September 2006 - 08:06 PM

I found how to open an url by php:
<?php header("Location: http://asite.com/file.html");?>
I think I found my answers for now.
Thanks.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users