PHP变量存在mySQL问题

时间:2018-09-24 18:49:43

标签: php mysql

这是我的一些PHP和SQL语句,用于将变量$ d添加到mySQL表中的现有值。

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。 有谁能够帮助我? 谢谢

1 个答案:

答案 0 :(得分:0)

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

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

此处的Math.用法类似于JS。在PHP .串联中,它假定您要使Math为字符串,以便将其串联为四舍五入的值13。这应该适合您:

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

您也应该使用参数化查询,尽管这不是当前的问题。

收到的错误消息是因为Mysql假定您正在尝试将2列的值加在一起,而找不到第二列。

这是一个演示问题https://3v4l.org/DeM62(已打开通知和警告)的链接。

要获取有用的错误,请参阅How do I get PHP errors to display?