Adding 1 to Column Value

This is a discussion on "Adding 1 to Column Value" within the PHP Forum section. This forum, and the thread "Adding 1 to Column Value are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Feb 3rd, 2008, 08:39
Jack Franklin's Avatar
Resources Administrator

SuperMember
Join Date: May 2007
Location: Cornwall, England
Posts: 1,266
Blog Entries: 7
Thanks: 10
Thanked 4 Times in 4 Posts
Adding 1 to Column Value

Each row in my table 'content' stores a different post, and the end colum is called 'views'. When a post is viewed, I want to add 1 to the column 'Views', so I can display a list of the top viewed posts in the side bar. This is my current code:
PHP: Select all

$contentid $_GET['contentid'];

$views mysql_query("SELECT views FROM content WHERE contentid='$contentid'");
while (
$no_views mysql_fetch_array($views)) {
$views_before $no_views['views'];
$views_after $views_before+1;
$updateviews "UPDATE content SET
views='$views_after'
WHERE contentid='$contentid'"
;
if(
$updateviews) {
echo 
'OK';
} else {
echo 
'NO';
}

When I run it it seems not to run, I don't see 'OK' or 'NO', and I surely should be seeing one or the other?
I bets it's some stupid mistake. I'm willing to bet it's this line:
PHP: Select all

$views_after $views_before+1
But even if that failed I should still be seeing an error?

Thanks,

Jack
Last Blog Entry: My Latest Project - Grilling Gurus... (Jun 11th, 2008)
Reply With Quote

  #2 (permalink)  
Old Feb 3rd, 2008, 09:19
Jack Franklin's Avatar
Resources Administrator

SuperMember
Join Date: May 2007
Location: Cornwall, England
Posts: 1,266
Blog Entries: 7
Thanks: 10
Thanked 4 Times in 4 Posts
Re: Adding 1 to Column Value

Ok. I was uploading it to the wrong place! DUH! But there is still a problem:
PHP: Select all

$contentid $_GET['contentid'];
$views mysql_query("SELECT views FROM content WHERE contentid='$contentid'");
while (
$no_views mysql_fetch_array($views)) {
$views_before $no_views['views'];
$views_after $views_before+1;
echo 
$views_before;
echo 
$views_after;
$updateviews "UPDATE content SET
views='$views_after'
WHERE contentid='$contentid'"
;
if(
$updateviews) {
echo 
'NO';
} else {
echo 
'NO';
}

When you load the page it echos the variables $views_before & $views_after. That always returns '0 1' because the data is not being added to the table content. Therefore there must be an error with the MySQL bit. Ideas please!
Last Blog Entry: My Latest Project - Grilling Gurus... (Jun 11th, 2008)
Reply With Quote
  #3 (permalink)  
Old Feb 3rd, 2008, 10:08
Jack Franklin's Avatar
Resources Administrator

SuperMember
Join Date: May 2007
Location: Cornwall, England
Posts: 1,266
Blog Entries: 7
Thanks: 10
Thanked 4 Times in 4 Posts
Re: Adding 1 to Column Value

i'm now guessing it might be the table structure? If it helps, here it is:

Also, if you look at any post on my site, you'll see the text: 01Y above the title. This says the views before, views after, and then if the sql UPDATE has worked. So, it's saying it has?
Last Blog Entry: My Latest Project - Grilling Gurus... (Jun 11th, 2008)
Reply With Quote
  #4 (permalink)  
Old Feb 3rd, 2008, 10:14
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Adding 1 to Column Value

Umm... You're really overcomplicating the matter

You don't need to fetch anything from the database at all. You have the views with a default value of 0 so all you need to do is.

PHP: Select all



// Update the views

mysql_query("UPDATE content SET views = views+1 WHERE contentid=$contentid");

// Then retrieve the data 

$result mysql_query("SELECT views FROM content WHERE contentid=$contentid");

$row mysql_fetch_assoc($result);

// echo views before

echo $row['views']-1;

// echo views after

echo $row['views']; 

That will update it without having to bring PHP into the story at all MYSQL is much quicker than PHP so whenever you can use mysql's functions and logic, use it.
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
  #5 (permalink)  
Old Feb 3rd, 2008, 10:19
Jack Franklin's Avatar
Resources Administrator

SuperMember
Join Date: May 2007
Location: Cornwall, England
Posts: 1,266
Blog Entries: 7
Thanks: 10
Thanked 4 Times in 4 Posts
Re: Adding 1 to Column Value

Bloomin hell, why the hell did I do all of that when I needed just one line. Thanks Rakuli. I'll try it right away

I LOVE YOU RAKULI! Thanks a lot, I cannot belive I spent a good hour bashing my head on the desk when I only needed one bloomin line.
Last Blog Entry: My Latest Project - Grilling Gurus... (Jun 11th, 2008)
Reply With Quote
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding a column to a table Jack Franklin Databases 2 Feb 3rd, 2008 08:17
SQL Syntax for adding multiple rows together from 1 column jfergy Databases 3 Jan 17th, 2006 04:12
rename column name simonneaves Databases 2 Nov 11th, 2005 00:08
..copy data from column A in Table A to column B in Table B? gecastill Databases 10 Jun 23rd, 2005 18:27


All times are GMT. The time now is 22:08.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43