Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8508025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:02:24+00:00 2026-06-11T03:02:24+00:00

Currently, I have the following PHP code loaded every time the page is refreshed.

  • 0

Currently, I have the following PHP code loaded every time the page is refreshed. I am trying to update the views column +1 every time the page is loaded. To do this, I first retrive the previous views value from the table, then run another query to add + to that number. The problem that is occurring is every time I refresh the page, The code somehow adds two instead of 1. So instead of the $viewsA variable increasing by +1, it is increasing by +2.

$query = mysql_query("SELECT * FROM Games WHERE pagename = '$game' ");

WHILE($datarows = mysql_fetch_array($query)):

    $title = $datarows['title'];
    $description = $datarows['desc'];
    $img_url = $datarows['img'];
    $cat = $datarows['cat'];
    $pagename = $datarows['page'];
    $rating = $datarows['rat'];
    $viewsA = $datarows['view_count'];
    $gameid = $datarows['id'];

endwhile;

$updateviews = $viewsA +1;


mysql_query("UPDATE  `trainw_games`.`Games` SET  `view_count` =  '$updateviews' WHERE  `Games`.`id` = $gameid;");

What do I need to change to make it only add +1 to the views column?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T03:02:25+00:00Added an answer on June 11, 2026 at 3:02 am

    I don’t think that a while loop is appropriate for this problem. I would recommend to echo $viewsA . '-' . $updateviews; to see what the value is before and after the add.

    But, why not just run a single UPDATE statement?

    UPDATE Games SET view_count = view_count + 1 WHERE Games.id = $gameid
    

    Of course, you should stop using mysql_ functions and use either MySQLi or PDO:

    $stmt = $mysqli->prepare("UPDATE Games SET view_count = view_count + 1 WHERE Games.id = ?"); 
    $stmt->bind_param($gameid);
    $stmt->execute();
    $stmt->close(); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have currently the following code: <li><a href=index.php id=1 onclick=document.getElementById('1').style.background = '#8B4513';>Weblog</a></li> This changes
Currently (in PHP) I have the following regex pattern: \[(.*) (.*)=(.*)\] This matches [doSomething
I currently have the following piece of code. When true is returned from tv.php
I currently have the following PHP code to control the visibility of a block:
I currently have the following code: <ul id=menu-list> <a href=index.php> <li>Home</li> </a> <li><a href=about.php>About</a></li>
I currently have this code. <?php $username = 'EMAIL ADDRESS'; $password = 'EMAIL ADDRESS';
currently I have the following code inside my 'function.php' is function calcTime($database_name,$currentTime){ global $startTime;
currently I have following code: home.php <form name='myformname' id='myformid'> <input type='text' name='mytext1' value='abc'> <input
I currently have the following htaccess rewrite rule: RewriteRule (.*) index.php/$1 [L] How do
I currently have the following code which delays showing a fixed bar after a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.