SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict svcghost's Avatar
    Join Date
    Oct 2010
    Posts
    288
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP validation vs MySQL validation

    Hey guys,

    What is more efficient in this example? PHP script inputs data into MySQL database. Should I first check to see if an identical record exists (don't want identical) with PHP by doing a SELECT statement? Or should I just set the field to be UNIQUE in MySQL and just let the user try to input identical data but fail thanks to MySQL?

    I'm wondering which is more efficient. Using PHP to check via mysql_query every time seems exhaustive especially if users don't try and input identicals. But what if I just let MySQL reject the data? Is that a performance hogger? Or is this the ideal approach?

    Thanks guys!

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,250
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by svcghost View Post
    Should I first check to see if an identical record exists (don't want identical) with PHP by doing a SELECT statement? Or should I just set the field to be UNIQUE in MySQL and just let the user try to input identical data but fail thanks to MySQL?
    the latter

    it's twice as efficient, as it does only one database call instead of two
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    As Rudy says, you can let it fail at the database.

    Don't forget, however, to catch the database's error message so that you can tell the user why it failed.

  4. #4
    SitePoint Addict svcghost's Avatar
    Join Date
    Oct 2010
    Posts
    288
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good point! Thanks for the reminder.

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    mysql err no. 1062 Message: Duplicate entry '%s' for key %d

    MySQL :: MySQL 5.5 Reference Manual :: C.3 Server Error Codes and Messages

  6. #6
    SitePoint Member
    Join Date
    Jun 2013
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey great info everyone - even though I know I'm a little late to the party. So far it works perfectly. What I'm trying to do is verify whether a street address has been previously inputted or not.

    Checking UNIQUE works just fine when the address is all contained in one input box (on html form) and one row of the table, but I'd like to standardize it a bit more with street address, city, state, country, zip all having their own input box (on the html form).

    I'm wondering what is the best practice for something like this. Would I have each html field as a populate a separate row in the table? Or could I combine all the data from the different input boxes (of the html form) and have them populate one row in the table?

    Thanks in advance,
    Stephen


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
  •