Jump to content

How to handle MySQL errors ?


adrianTNT

Recommended Posts

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
[b]"Duplicate entry 'Adrian' for key 2".[/b]

How can I handle these errors so that if the username is already taken then the visitor is sent to
[b]message.php?message=Username Taken[/b]
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:

[code]

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));
}

[/code]

Link to comment
Share on other sites

[quote author=fenway link=topic=106526.msg426084#msg426084 date=1157132604]
Well, you have a die() statement -- just write a custom handler.
[/quote]
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:
[b]Open url : message.php?message=Error, user already exists.[/b]
How can I open the URL by php code?

Thank you.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.