Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-24-2012, 09:14 PM   PM User | #1
Dan13071992
Regular Coder

 
Join Date: Dec 2010
Location: Kent, UK
Posts: 573
Thanks: 23
Thanked 10 Times in 10 Posts
Dan13071992 is an unknown quantity at this point
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
Dan13071992 is offline   Reply With Quote
Old 04-24-2012, 09:33 PM   PM User | #2
myfayt
Senior Coder

 
Join Date: Apr 2010
Posts: 1,159
Thanks: 46
Thanked 96 Times in 95 Posts
myfayt can only hope to improve
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 6 years. My business:
American Made Signs
myfayt is offline   Reply With Quote
Old 04-24-2012, 09:35 PM   PM User | #3
Dan13071992
Regular Coder

 
Join Date: Dec 2010
Location: Kent, UK
Posts: 573
Thanks: 23
Thanked 10 Times in 10 Posts
Dan13071992 is an unknown quantity at this point
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
Dan13071992 is offline   Reply With Quote
Old 04-25-2012, 09:06 AM   PM User | #4
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,907
Thanks: 10
Thanked 293 Times in 289 Posts
Dormilich is on a distinguished road
something like INSERT INTO ... ON DUPLICATE KEY UPDATE ... ?
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 04-25-2012, 09:09 AM   PM User | #5
Dan13071992
Regular Coder

 
Join Date: Dec 2010
Location: Kent, UK
Posts: 573
Thanks: 23
Thanked 10 Times in 10 Posts
Dan13071992 is an unknown quantity at this point
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
Dan13071992 is offline   Reply With Quote
Old 04-25-2012, 09:10 AM   PM User | #6
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,907
Thanks: 10
Thanked 293 Times in 289 Posts
Dormilich is on a distinguished road
a whole explanation
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 04-25-2012, 09:12 AM   PM User | #7
Dan13071992
Regular Coder

 
Join Date: Dec 2010
Location: Kent, UK
Posts: 573
Thanks: 23
Thanked 10 Times in 10 Posts
Dan13071992 is an unknown quantity at this point
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
Dan13071992 is offline   Reply With Quote
Old 04-25-2012, 11:20 AM   PM User | #8
Dan13071992
Regular Coder

 
Join Date: Dec 2010
Location: Kent, UK
Posts: 573
Thanks: 23
Thanked 10 Times in 10 Posts
Dan13071992 is an unknown quantity at this point
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
Dan13071992 is offline   Reply With Quote
Old 04-25-2012, 03:26 PM   PM User | #9
Dan13071992
Regular Coder

 
Join Date: Dec 2010
Location: Kent, UK
Posts: 573
Thanks: 23
Thanked 10 Times in 10 Posts
Dan13071992 is an unknown quantity at this point
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?
__________________
http://360-tactics.co.uk/forum/index.php

Crime-Wave

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

Last edited by Dan13071992; 04-25-2012 at 04:04 PM..
Dan13071992 is offline   Reply With Quote
Old 04-26-2012, 05:45 PM   PM User | #10
myfayt
Senior Coder

 
Join Date: Apr 2010
Posts: 1,159
Thanks: 46
Thanked 96 Times in 95 Posts
myfayt can only hope to improve
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 6 years. My business:
American Made Signs
myfayt is offline   Reply With Quote
Old 04-26-2012, 06:48 PM   PM User | #11
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,907
Thanks: 10
Thanked 293 Times in 289 Posts
Dormilich is on a distinguished road
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
Dormilich is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:13 AM.


Advertisement
Log in to turn off these ads.