我试图将一个值增加 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/