Enjoy an ad free experience by logging in. Not a member yet?
Register .
04-24-2012, 09:14 PM
PM User |
#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
04-24-2012, 09:33 PM
PM User |
#2
Senior Coder
Join Date: Apr 2010
Posts: 1,159
Thanks: 46
Thanked 96 Times in 95 Posts
We need more information on what you're trying to do. Are you looking to match like a username or what? Thanks
04-24-2012, 09:35 PM
PM User |
#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
04-25-2012, 09:06 AM
PM User |
#4
Senior Coder
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,907
Thanks: 10
Thanked 293 Times in 289 Posts
something like INSERT INTO ... ON DUPLICATE KEY UPDATE ... ?
__________________
please post your code wrapped in [CODE] [/CODE] tags
04-25-2012, 09:09 AM
PM User |
#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
04-25-2012, 09:10 AM
PM User |
#6
Senior Coder
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,907
Thanks: 10
Thanked 293 Times in 289 Posts
__________________
please post your code wrapped in [CODE] [/CODE] tags
04-25-2012, 09:12 AM
PM User |
#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
04-25-2012, 11:20 AM
PM User |
#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?
04-25-2012, 03:26 PM
PM User |
#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 ..
04-26-2012, 05:45 PM
PM User |
#10
Senior Coder
Join Date: Apr 2010
Posts: 1,159
Thanks: 46
Thanked 96 Times in 95 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" );
04-26-2012, 06:48 PM
PM User |
#11
Senior Coder
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,907
Thanks: 10
Thanked 293 Times in 289 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");
__________________
please post your code wrapped in [CODE] [/CODE] tags
Jump To Top of Thread
Thread Tools
Rate This Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT +1. The time now is 02:13 AM .
Advertisement
Log in to turn off these ads.