简体   繁体   English

无法将数据写入数据库

[英]Trouble writing data to database

I wrote the following script. 我写了以下脚本。 My intention was to do the two following things: 我的意图是做以下两件事:

  1. Let a user see the current value of $score . 让用户看到$score的当前值。
  2. Let the user increment the $score by 1 by pressing the "Score!" 让用户通过按“ Score!”将$score加1。 button. 按钮。

The value of $score is stored in a database. $score的值存储在数据库中。

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. 按钮, $score的值不会增加一-它将重置为零,无论原始值是多少。

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

Because your query is in single quotes which makes your variable $scores become the literal word $scores. 因为查询用单引号引起,所以变量$scores变成了文字$ scores。 As a result your number data type (prob int) is converting it to a zero. 结果,您的数字数据类型(prob int)将其转换为零。

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. 我使用串联来确保在查询中使用$scores的值,而不是使用文字字面的$ scores。

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

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

replace 更换

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

with

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

it is taking $score as word, which doesn't exist in DB 它以$ score作为单词,这在数据库中不存在

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM