Checking database for already inputted value

Discussion in 'PHP' started by Ipodman79, Jul 12, 2014.

  1. #1
    I have a site where the user enters in a "item". It is then stored. They can then view it and edit it etc. I don't want them to be able to add in the same item twice, e.g. "apples".

    I was thinking of when the submit the form checking it by selecting the row with the same name as $_POST['item_name'] and if it didn't return any rows then allow the script to continue.

    If it did return a row then terminate the script.

    Is this the best way of doing it or is there a better and easier way?
     
    Ipodman79, Jul 12, 2014 IP
  2. Tony Toreto

    Tony Toreto Member

    Messages:
    35
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    28
    #2
    make primary key on item_name so that it never duplicate..it is the easiest solution..
     
    Tony Toreto, Jul 12, 2014 IP
  3. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #3
    And that won't work if there are several users, and the block is meant to be pr. user, not pr. item.
    Depends on the needs.
    If you just need to check pr. user, do a SELECT-statement where you check for the content of item-name, and if it exists, you can maybe give the user the opportunity to directly update that entry, instead of storing a new entry.
     
    PoPSiCLe, Jul 12, 2014 IP
  4. Ipodman79

    Ipodman79 Greenhorn

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    6
    #4
    I already have an id column as my primary key.
     
    Ipodman79, Jul 12, 2014 IP
  5. Tony Toreto

    Tony Toreto Member

    Messages:
    35
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    28
    #5
    you can use more then 2 fields as combine primary key :)..
     
    Tony Toreto, Jul 12, 2014 IP
  6. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #6
    ... or just set the column as "unique" - no need to make it a primary key...
     
    PoPSiCLe, Jul 12, 2014 IP
  7. Ipodman79

    Ipodman79 Greenhorn

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    6
    #7
    This wouldn't work for my users. If user A enters "apples" user B can't then add "apples". I want only the user to be not allowed enter the same thing twice. I don't care if two different users add the same item.
     
    Ipodman79, Jul 12, 2014 IP
  8. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #8
    Exactly - see my answer above...
     
    PoPSiCLe, Jul 12, 2014 IP