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 8329845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:55:52+00:00 2026-06-09T01:55:52+00:00

I wrote the following script. My intention was to do the two following things:

  • 0

I wrote the following script. My intention was to do the two following things:

  1. Let a user see the current value of $score.
  2. Let the user increment the $score by 1 by pressing the “Score!” button.

The value of $score is stored in a database.

Only one problem — when I click the “Score!” button, the value of $score doesn’t get incremented by one — it gets reset to zero, regardless of what the original value is.

<?php
    $page_title = "";
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title><?php print($page_title) ?></title>
    </head>
    <body>

    <?php // Script number 1.2, filename show_score.php

    // error handling
    ini_set('display errors',1);  // Let me learn from my mistakes!
    error_reporting(E_ALL|E_STRICT); // Show all possible problems! 

    // Set page title. 
    $page_title = "Game Score";

    // Connect to the database:
    $link = mysql_connect('localhost','username','password');
    mysql_select_db('game_scores',$link);

    // Create database query that gets the value of the score_counter cell
    $sql = 'SELECT score_counter FROM scores';
    $result = mysql_query($sql,$link);

    // Create a variable, $score_counter, that holds the array that 
    //is the result of the previous SQL query

    $score_counter = mysql_fetch_array($result);

    // You don't really want the whole array, just score_counter[0],
    // so assign score_counter[0] its own variable, $score

    $scores = $score_counter[0];

    // Now that you've retrieved the current number of scores from the 
    // database, and extracted that number from an array and 
    // put it in its own variable, print it out on the screen.

    echo 'The current number of scores is ' . $scores . ' scores.';


    // Now let users add to the number of scores by clicking the "score" button. 

    if(isset($_POST['score'])){

        // increment the number of scores:
        $scores++;

        // create the SQL query:
        $query='UPDATE scores SET score_counter="$scores" WHERE score_id=1';

        // run the SQL query:
        mysql_query($query) or die("Cannot update");
        }

    ?>

    <H1>Score Counter</H1>

    <p>Click to add one point.</p>

    <form action ="show_score.php" method ="post">
    <input type ="submit" name ="score" value="score">
    </form>

    </body>
    </html>
  • 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-09T01:55:53+00:00Added an answer on June 9, 2026 at 1:55 am

    Because your query is in single quotes which makes your variable $scores become the literal word $scores. As a result your number data type (prob int) is converting it to a zero.

    Change your query to this:

    $query='UPDATE scores SET score_counter="'.$scores.'" WHERE score_id=1';
    

    I used concatenation to make sure the value of $scores was used in the query instead of of the literal word $scores.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote the following pre-commit script for SVN to validate that a user has
I wrote the following html file <code> <html> <head> <script text =text/javascript> function createCookie(name,value,days)
I wrote the following script so that an end-user can create a word document
I wrote the following script, which generates a SyntaxError : #!/usr/bin/python print Enter the
I have an interesting problem. I wrote the following perl script to recursively loop
following is the bash script I wrote for Sqlite. #!/bin/bash sqlite3 file.db CREATE TABLE
I wrote the following Perl script (below) in order to create simple XML file.
I'm a bit confused here. I wrote the following script to add files of
I wrote the following script that works fine in my SQL Server 2008 developer
I wrote the following script to avoid exposing the protected files to users logged

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.