www.webdeveloper.com
Results 1 to 5 of 5

Thread: [RESOLVED] IF EXISTS UPDATE ELSE INSERT... throwing me for a loop?

  1. #1
    Join Date
    Oct 2013
    Posts
    16

    resolved [RESOLVED] IF EXISTS UPDATE ELSE INSERT... throwing me for a loop?

    I will try to put this as simple as possible. I have a section of code that is using session id to locate table row with same id to update and view database table info. How ever i would like the form to display and when user hits button labeled make changes or update if the table row does not exist with session id would like for the code to create and insert data and then next time only update?
    for example

    user is on a page that asks them what they would like to do they click on a link then are redirected to another page. on this page they see a form to fill out and when they hit enter or click update button it inserts data to table with session id so that the next time when they come back to this form they can see what they put init last time and can make updates to it with out the database making a duplicate entry

    not sure if i should be using ' on duplicate ' or ' if not exists ' looking for something search online tried various options and nothing seems to work.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,523
    Assuming MySQL (not all DBMS's support this, necessarily), first make sure there's a unique index on the column(s) in question. Then you can use the INERT ... ON DUPLICATE KEY UPDATE syntax to let the DB handle it.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Dec 2002
    Location
    Seattle, WA
    Posts
    1,843

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,523
    Ah, that's right: I think it's MySQL's "replace" that is non-standard (but helluva convenient), whereas the "insert...on duplicate key update" is more generally supported. (I've been in PDO/PostgreSQL land for so long now that I'm pretty rusty with MySQL. )
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Oct 2013
    Posts
    16
    Thanks for the suggestions. Unfortunately the "Replace" and the "on Dup" functions where not what i had in mind. However i did find the solution starring me in the face. I just decided to use an insert into select function at the registration form to create the tables using the users id something i should have thought at the begging but hay im am after all just a newb. Thanks again.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles