php变量的mysql问题

798qvoo8  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(408)

这是我的一些php和sql代码,用于向mysql表中的现有值添加变量$d。

if ($a != 0 && $b != 0) {
  $c = 4 * ($a + $b);
  $d = Math.round($c * 1.1 / $a);
  $e = Math.round($c / $b);
}
$sql1 = "UPDATE login SET Punteggio = Punteggio + $d WHERE Risultato = $score";

已经定义了$a和$b。该页返回以下错误:

Error: SELECT Punteggio, Risultato FROM login
Unknown column 'Math13' in 'field list'

完整代码:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8"/>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
  <link href='https://fonts.googleapis.com/css?family=Montserrat:400,500' rel='stylesheet' type='text/css'>
  <link rel="stylesheet" type="text/css" href="https://prova2prova1.altervista.org/main1.css" id="theme">
  <link rel="shortcut icon" href="images/favicon.ico">
</head>

<body>

<?php

$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "prova";
$port = 8889;
$conn = mysqli_init();
if (!$conn)
{
die("mysqli_init failed");
}
if (!mysqli_real_connect($conn,$servername,$username,$password,$dbname,$port))
{
die("Connect Error: " . mysqli_connect_error());
}
$username1 = $_POST['Username'];
$password1 = $_POST['Password'];
$score = $_POST['score'];
$sql = "SELECT Punteggio, Risultato FROM login";
$result = $conn->query($sql);
$a = 0;
$b = 0;
while($row = $result->fetch_assoc()) {
  if ($row['Risultato'] == $score) {
    $a++;
  } else {
    $b++;
  }
}
if ($a != 0 && $b != 0) {
  $c = 4 * ($a + $b);
  $d = Math.round($c * 1.1 / $a);
  $e = Math.round($c / $b);
}
$sql1 = "UPDATE login SET Punteggio = Punteggio + $d WHERE Risultato = $score";
$sql2 = "UPDATE login SET Punteggio = Punteggio - $e WHERE NOT Risultato = $score";
if ($conn->query($sql1) == TRUE && $conn->query($sql2) == TRUE) {
echo 'I risultati sono stati aggiornati';
} else {
die("Error: " . $sql . "<br>" . $conn->error);
}
$conn->close();

?>

<form method="post" action="admin.php">
  <input hidden type="text" name="Username" value="<?php echo htmlspecialchars($username1); ?>">
  <input hidden type="password" name="Password" value="<?php echo htmlspecialchars($password1); ?>">
  <input type="submit" value="Home">
</form>

</body>

注意,在所有操作之后,$d等于13。有人能帮我吗?谢谢

ippsafx7

ippsafx71#

您的问题始于您没有使用错误报告这一事实。它在这条线上延伸:

$d = Math.round($c * 1.1 / $a);

这个 Math. 这里的用法看起来像js。在php中 . 连接,它假设你的意思是 Math 是字符串,因此它将该字符串连接到舍入值,即 13 . 这应该是您的:

$d = round($c * 1.1 / $a);

您还应该使用参数化查询,尽管这不是您当前的问题。
您收到的错误消息是因为mysql假设您正在尝试将两列值添加到一起,但它找不到第二列。
这里有一个链接演示了这个问题https://3v4l.org/dem62 (打开通知和警告)。
要获取有用的错误,请参阅如何获取要显示的php错误?。

相关问题