Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Mar 2008
    Posts
    61
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Detecting if row value already exists

    So I am running the following command.

    INSERT INTO alert (id, message)
    VALUES ('$id', 'test')

    So say if $id = "123" , it will incert a record of 123 into the ID box.

    How do I basically say "if the value of $id already exist in the row of alert in the ID field, than echo "ID already exist in database"

  • #2
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    If you just want it to not take the action of inserting into the DB you could use IGNORE.

    Code:
    INSERT IGNORE INTO alert (id, message) 
    VALUES ('$id', 'test')
    This will insert the record if the id does not exist and ignore it if it does. Alternatively you could use REPLACE if you wanted to repace the current database value. My code assumes that ID is a primary or unique key.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #3
    New Coder
    Join Date
    Mar 2008
    Posts
    61
    Thanks
    2
    Thanked 0 Times in 0 Posts
    What I want to happen is if , say if they say ?id=123 , if 123 already is in the record of "ID", than echo something like "ID Already exist in table" or something of that. So what I want to happen is it will look for the ID that was given in the $_GET function (lets say 123) , and if 123 already exist, run echo something. thats all I wnat to do is have it hunt and search.
    Last edited by WebmasterLULZ; 03-16-2008 at 02:25 AM.

  • #4
    New Coder
    Join Date
    Mar 2008
    Posts
    61
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Let me rephrase my question:

    I want to acomplish this

    IF 123 already excist in rowname "ID" under tablename "alert", than do nothing and just echo, else , do the query

  • #5
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    You would have to use another query to check the database beforehand.
    PHP Code:
    $result mysql_query('SELECT COUNT(*) FROM `table` WHERE `id` = 123');
    $count mysql_result($result0);
    if(
    $count == 1)
    {
        echo 
    'Row exists already';


  • #6
    New Coder
    Join Date
    Dec 2005
    Posts
    35
    Thanks
    1
    Thanked 1 Time in 1 Post
    This may not help much but I often use an extended version of Inigoesdr's code, its not what you asked for, but I will post it in case somebody finds it useful:

    PHP Code:
    $result mysql_query('SELECT COUNT(*) FROM `table` WHERE `id` = 123');
    $count mysql_result($result0);
    if(
    $count == 1)
    {
         
    UPDATE alert SET (idmessageVALUES ('$id''test'WHERE xxx
    }else{

         
    INSERT INTO alert (idmessageVALUES ('$id''test')



  • #7
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    If you were going to update it anyway you should just use REPLACE INTO, so you only need one query.

  • #8
    New Coder
    Join Date
    Dec 2005
    Posts
    35
    Thanks
    1
    Thanked 1 Time in 1 Post
    heheh thats why I love this forum. I love learning even when I'm the one trying to help :P


  •  

    Posting Permissions

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