php - 返回index.php时不执行以增量1增加的更新语句

标签 php mysql sql

我试图将一个值增加 1,该值存储在数据库中。 但是,当 add.php 文件运行并且我返回到 index.php 文件时,该值保持不变,没有任何增加的增量。

index.php

<html>
    <head>
        <p><a href = "add.php">Click to update score</a></p>
    </head>
</html>

<?php

    $mysqli = new mysqli ("localhost", "root", "", "friends");

    if ($mysqli -> connect_errno) {
        die('Connect Error: ' . $mysqli -> connect_errno);
    }

    $results = $mysqli -> query ("SELECT score FROM user LIMIT 1");
    while ($row = $results -> fetch_assoc()) {
    echo $row['score'];
    }

?>

add.php

<?php
    // Connect to the database with '$mysqli' as the connection variable name
    $mysqli = new mysqli ("localhost", "root", "", "friends");

    //Check connection
    if ($mysqli -> connect_errno) {
        die('Connect Error: ' . $mysqli -> connect_errno);
    }

     $result = mysqli_query($mysqli, "SELECT * FROM user LIMIT 1");
     $row = mysqli_fetch_array($result);


     //Update Statement
     $stmt = "UPDATE score SET score = score + 1 WHERE id = 1";


     //Result notice
     echo "Update completed.."; 
     //Link back to index.php
     echo "<a href = 'index.php'>Back to likes</a>";

?>

最佳答案

您实际上并未运行更新查询。您只是将语句设置在变量中。

//Update Statement

$stmt = "UPDATE score SET score = score + 1 WHERE id = 1";
$mysqli->query($stmt);

另一个选项是:

$stmt = "UPDATE score SET score = score + 1 WHERE id = 1";
$result = mysqli_query($mysqli, $stmt);

关于php - 返回index.php时不执行以增量1增加的更新语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43956956/

相关文章:

php - Unity和MySQL无法注册很多中文

mysql - Symfony2 和 Doctrine Mysql 测试数据库 Persist

php - 如果 mysql 数据库中存在,则不提交表单

sql - 通过 Perl 连接到 Teradata

sql - 计算 UNIQUE 匹配值数量的语法

php - 使用 PHP 将行添加到 MySQL 数据库但没有来自 html 表单的其他数据

php - strtotime 需要什么来生成字符串/默认值是什么?

javascript - 如何在 WordPress `functions.php` 中将一个又一个脚本排队,并让它们一起工作?

php - 如何创建包含各种排序规则的 MySQL 表?

sql - 如何在 SQL SERVER 2008 中启用字母(A、B、C、D...)自动递增?