#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Colombo, Western, Sri Lanka
    Posts
    41
    Rep Power
    15
    The 'name' column in my table is a UNIQUE column. Therefore the table will not create a new record if the name already exists.

    When I try to enter a name which already exists (entering using mysql) it returns an error which says :

    Error 2000: Duplicate entry 'plants' for key 4.

    Currently, the PHP file does not come up with an error message although the duplicate record has not been entered into the table.


    How can I get my PHP file to interpret the mysql error message and alert the user that this name already exists ??

    Please help.

    thnx.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Colombo, Western, Sri Lanka
    Posts
    41
    Rep Power
    15
    I've achieved what I originally wanted. The script searches the table for a match and then displays a message. If a match is not found it inserts the data.

    I'd like to know if there's another way where the mysql error on finding a duplicate can be picked up by the script...because in the mthod I've used a lot can happen if many uses are trying add data at the same time.

  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Bucharest, Romania
    Posts
    18
    Rep Power
    0
    As far as I know, you can't do this directly. You have to do first a query like: "select count(*) from table_name where name='$name'" (which is a pretty fast query); then: $count=mysql_result($result,0,0); if the variable $count is false (e.g. not 0) you can redirect to the file that contains the form using header function with an error message ("This name already exists in our database. Try another one").
  6. #4
  7. No Profile Picture
    RyanP
    Guest
    Devshed Newbie (0 - 499 posts)
    I think that a better error handling method would be to append a conditional die() function to the end of your query that will print out an error message if the query does not succeed, ex.<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>$result = mysql_query( ... )
    or die ("Query failed.");[/code]or you could also check to see if your query handler ($result) == 0 after running the query, and if so print error message and exit()

    [This message has been edited by RyanP (edited September 04, 2000).]

Similar Threads

  1. alternative to "on duplicate key update"
    By laker in forum MySQL Help
    Replies: 1
    Last Post: January 22nd, 2004, 11:22 AM
  2. PHP update record selects the first record only
    By dwmartin in forum PHP Development
    Replies: 11
    Last Post: October 7th, 2003, 04:48 PM
  3. Ugly PHP script works expect on last record in MySQL table
    By arconian in forum PHP Development
    Replies: 0
    Last Post: September 5th, 2003, 06:46 AM
  4. Cronjob madness
    By eliteboo in forum Linux Help
    Replies: 10
    Last Post: July 14th, 2003, 11:49 AM
  5. Spotlight record delay
    By Harle in forum PHP Development
    Replies: 3
    Last Post: June 1st, 2003, 06:11 PM

IMN logo majestic logo threadwatch logo seochat tools logo