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 11 of 11
  1. #1
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts

    if doesnt exist then create, else just update

    hello everyone, im curious as to how i would go about writing a script that would create a row if it doesnt already exist, but if it does exist then just update that row.

    What would be the best way to do this?

    Thanks.

    Dan
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #2
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,408
    Thanks
    68
    Thanked 102 Times in 101 Posts
    We need more information on what you're trying to do. Are you looking to match like a username or what? Thanks
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #3
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    thanks for the reply.

    it would need to be constructed like this:

    if ($strTitle) doesnt exit in table create row, elseif it does exist uodate "views" +1 where pagename = $strTitle
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,237
    Thanks
    12
    Thanked 340 Times in 336 Posts
    something like INSERT INTO ... ON DUPLICATE KEY UPDATE ... ?
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #5
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    could you elaborate a little more please, maybe with a small example, as ive never used this before.

    thanks.

    Dan
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #6
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,237
    Thanks
    12
    Thanked 340 Times in 336 Posts
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #7
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    that is bang on exactly what i want thank you, ill report back with what i get and if it works or not

    thanks once again

    Dan
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #8
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    ok, so ive looked into it, and ive wrote it like this, however, it neither inserts, nor updates :S

    PHP Code:
    mysql_query("INSERT INTO pageviews (pagename,views) VALUES ($strTitle,0) ON DUPLICATE KEY UPDATE views=views+1"); 
    have i gone wrong somewhere?
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #9
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    ive tried the above mysql_query (from my previous post) on a couple of different pages, however i have still not managed to get it to work, any ideas why? am i doing it wrong?
    Last edited by Dan13071992; 04-25-2012 at 04:04 PM.
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #10
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,408
    Thanks
    68
    Thanked 102 Times in 101 Posts
    Hmm I believe you need single quotes in the values. I code these differently, but in this format, try:

    PHP Code:
    mysql_query("INSERT INTO pageviews (pagename,views) VALUES ('$strTitle','0') ON DUPLICATE KEY UPDATE views=views+1"); 
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #11
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,237
    Thanks
    12
    Thanked 340 Times in 336 Posts
    you need the quotes for the string value, integers are used without.
    Code:
    mysql_query("INSERT INTO pageviews (pagename, views) VALUES ('$strTitle', 0) ON DUPLICATE KEY UPDATE views=views+1");
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

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