无法将数据写入数据库

时间:2012-08-01 19:10:02

标签: php mysql

我写了以下脚本。我的目的是做以下两件事:

  1. 让用户查看$score
  2. 的当前值
  3. 让用户按“分数”将$score增加1按钮。
  4. $score的值存储在数据库中。

    只有一个问题 - 当我点击“分数!”时按钮,$score的值不会增加1 - 无论原始值是什么,它都会重置为零。

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

3 个答案:

答案 0 :(得分:5)

因为您的查询是单引号,这使得您的变量$scores成为字面词$ score。因此,您的数字数据类型(prob int)将其转换为零。

将您的查询更改为:

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

我使用连接来确保在查询中使用$scores的值而不是文字$ score。

答案 1 :(得分:0)

要在PHP字符串中使用变量插值,您需要使用双引号:

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

答案 2 :(得分:0)

替换

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

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

它将$得分作为单词,这在DB

中不存在