Prevent entering duplicate records to mysql table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • phopman
    New Member
    • Aug 2007
    • 26

    Prevent entering duplicate records to mysql table

    Hi there!

    Since this is my first post, I'll try to be gentle with you :-)

    I have a script that goes like this:

    Connect to db. select data from db, and list up (only one variable, text).

    I also have a form so I can insert into the database (insert into table where $id = contentid).

    How can I check in php, if it is already there?

    My code is something like this:

    [code=php]
    $id = intval( mysql_real_esca pe_string($_GET['id']));
    $as_text = addslashes($_PO ST['text']);
    $tr_text = trim($as_text);
    $query = "INSERT INTO jos_issues(ID,t ext,contentid)
    VALUES(NULL,'$t r_text','$id')" ;
    $result = mysql_query($qu ery);
    [/code]

    How can I easily check if it has already been inserted into the database?

    the table consists of 3 columns; id, text and contentid.
    contentid is used so I can list all text that is connected to one particular content.

    So, now I've been gentle with you, so you can be gentle with me :-)


    Phopman
    Last edited by ak1dnar; Aug 28 '07, 12:25 PM. Reason: Added [code=php]
  • ak1dnar
    Recognized Expert Top Contributor
    • Jan 2007
    • 1584

    #2
    Welcome to TheScripts.com

    Your "ID" column is an auto increment field correct?
    Then no Need to insert a Null value to it. remove it first.

    And back to the question, As I feel you are trying to prevent entering duplicate records to the table.
    But when you need it to check? in other words, duplicate entries could be store to the table when pressing refresh button after inserting record(when on the same page) or is it any other instance?

    Comment

    • phopman
      New Member
      • Aug 2007
      • 26

      #3
      Originally posted by ajaxrand
      Welcome to TheScripts.com

      Your "ID" column is an auto increment field correct?
      Then no Need to insert a Null value to it. remove it first.

      And back to the question, As I feel you are trying to prevent entering duplicate records to the table.
      But when you need it to check? in other words, duplicate entries could be store to the table when pressing refresh button after inserting record(when on the same page) or is it any other instance?
      I am trying to prevent duplicate records. one text (ex. 'Heidi',
      belongs only to one contentID.

      I need to check whenever someone press submit after filling out the form, or whenever someone press refresh button.

      How do I do that? do you have any simple examples of this problem? I couldn't find any good examples on google.com, but I don't think I am the first one with a problem like this.


      Phopman

      Comment

      • ak1dnar
        Recognized Expert Top Contributor
        • Jan 2007
        • 1584

        #4
        There were few threads on the site, I'm sure. I'll try to find out one for you. keep posting with your updates, then more experts comes to the thread. Please use the site search tool to find out.

        Thread Title changed to match with the question:
        Update delete and a select from db php. How?

        Comment

        • pbmods
          Recognized Expert Expert
          • Apr 2007
          • 5821

          #5
          Heya, Phopman.

          Try using REPLACE instead of INSERT.

          REPLACE will automatically remove the conflicting row if it exists before INSERTing the new data.

          Comment

          • phopman
            New Member
            • Aug 2007
            • 26

            #6
            It works!

            Thanks for the tips. It works like a charm!
            Last edited by phopman; Aug 29 '07, 10:38 AM. Reason: change it to [solved]

            Comment

            • pbmods
              Recognized Expert Expert
              • Apr 2007
              • 5821

              #7
              Heya, Phopman.

              Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)

              Comment

              • phopman
                New Member
                • Aug 2007
                • 26

                #8
                Will do!

                I just found this forum, and I hope I can contribute more in time than I am doing now.

                Real friendly forum with nice people it seems.

                You are people and not computers? :-)

                Comment

                • pbmods
                  Recognized Expert Expert
                  • Apr 2007
                  • 5821

                  #9
                  I'm sorry, Dave.

                  I'm afraid I can't answer that.

                  Comment

                  • phopman
                    New Member
                    • Aug 2007
                    • 26

                    #10
                    What are you talking about, HAL?

                    Comment

                    Working...