-2

this is some of my PHP and SQL to add a variable, $d, to an existing value in my mySQL table.

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 and $b have already been defined. The page returns this error:

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

Complete code:

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

Notice that after all the operations $d is equal to 13. Can anybody help me? Thanks

  • 3
    1) Some of that isn't PHP. 2) Your error doesn't match the code you've provided, so the issue is probably elsewhere. – Jonnix Sep 24 '18 at 18:52
  • Check your database table for the structure, the error is referencing to a column that maybe isn't defined. Post also a snippet of your `SELECT` query so we can give you a reply about. –  Sep 24 '18 at 18:55
  • Yep,you're right. Just OP treating PHP as a different language causing bugs. – Jonnix Sep 24 '18 at 19:15
  • So how can I correct the code? I tried to put numbers instead of $d and $e in the SQL statement and it works. – Valerio Stancanelli Sep 24 '18 at 19:16

1 Answers1

0

Your issues starts with the fact that you are not using error reporting. It extends at this line:

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

The Math. usage here looks like JS. In PHP . concatenates, it assumes you meant for Math to be a string so it concatenates that to the rounded value, which is 13. This should round for your:

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

You also should use parameterized queries, although that isn't your current issue.

The error message you receive is because Mysql assumes you are trying to add 2 columns values together and it can't find the second column.

Here's a link demonstrating the issue https://3v4l.org/DeM62 (with notices and warnings turned on).

To get useful errors please see How do I get PHP errors to display?.

user3783243
  • 5,368
  • 5
  • 22
  • 41