SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict betrollwin.com's Avatar
    Join Date
    Nov 2005
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    intercepting mysql error code in php

    When someone enter info into a form, it gets uploaded into the mysql database. When a duplcate is found in a row declared as unique, I get error message:

    Duplicate entry 'testemail@g.com' for key 1

    How would I make it say something else like:

    You have entered an email already in our database.

  2. #2
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    $sql = "INSERT INTO table yada yada";

    if ( !@mysql_query ($sql) ) {
    echo "It looks like the query did a boo-boo. Please try again";
    }

    http://us3.php.net/manual/en/function.mysql-query.php
    Ian Anderson
    www.siteguru.co.uk

  3. #3
    SitePoint Addict
    Join Date
    Apr 2004
    Location
    Belgian in Mexico
    Posts
    307
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Before trying to insert the data into yoiur database, you can check if the e-mail already exists:
    PHP Code:
    $result mysql_query("SELECT * FROM table WHERE email='email@domain.com'");
    $num_rows mysql_num_rows($result);
    if(
    $num_rows == 1) {
        echo 
    "You have entered an email already in our database.";

    Regards,

    MichaŽl
    MichaŽl Niessen
    http://assemblysys.com
    (Countries/states/cities with latitude & longitude,
    weathercodes & topical databases)

  4. #4
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Ian Anderson
    www.siteguru.co.uk

  5. #5
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,273
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    MichaŽl, no, he just got finished a thread where it was pointed out that you do not need to do that -- for one thing, it's not necessary to do yourself what the database can easily and more efficiently do, and for another thing, it doubles your processing time
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •