1

以下のスクリプトを書きました。私の意図は、次の2つのことを行うことでした。

  1. の現在の値をユーザーに表示します$score
  2. ユーザー$scoreが「Score!」を押して を 1ずつ増やします。ボタン。

の値は$scoreデータベースに保存されます。

1 つだけ問題があります。[Score!] をクリックしたときです。ボタンをクリックすると、 の値は$score1 ずつ増加しません。元の値に関係なく、ゼロにリセットされます。

<?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>
4

3 に答える 3

5

クエリが一重引用符で囲まれているため、変数$scoresがリテラル ワード $scores になります。その結果、数値データ型 (prob int) がゼロに変換されます。

クエリを次のように変更します。

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

連結を$scores使用して、リテラル ワード $scores の代わりに の値がクエリで使用されていることを確認しました。

于 2012-08-01T19:12:05.687 に答える
0

PHP 文字列で変数補間を使用するには、二重引用符を使用する必要があります。

$query="UPDATE scores SET score_counter=$scores WHERE score_id=1";
于 2012-08-01T19:13:07.480 に答える
0

交換

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

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

DBに存在しない単語として$scoreを取っています

于 2012-08-01T19:13:46.883 に答える