Update the database by SQL PHP

0

Personal I'm having difficulty updating my database to decrease the weight by 1 kg. Any help I appreciate. The code is as follows:

<?php
    class Aluno{
        public $nome;
        public $endereco;
        public $peso;
        public $altura;

            function __construct($nome, $endereco, $peso, $altura, $imc, $programa) {
            $this->nome = $nome;
            $this->endereco = $endereco;
            $this->peso = $peso;
            $this->altura = $altura;
            $this->imc = $imc;
            $this->programa = $programa;
            $this->imc = $this->calcularIMC();

            $this->peso = $this->calcularPeso();

      if ($this->imc<17) {$this->programa = "de ganho de peso.";
      }
      elseif ($this->imc>30) {$this->programa = "de perda de peso.";
      }
      else {$this->programa = "de manutenção do peso.";
      }


}

    function calcularPeso() {return round(($this->peso - 1),2);}

    function calcularIMC() {return round(($this->peso)/($this->altura*$this->altura),2);}



}
    $conexao = mysqli_connect("localhost", "root", "", "academia");

    if(mysqli_connect_errno($conexao))
    {
        echo "Não conectado!<br>";
    }

    else {
        mysqli_query($conexao, "SET NAMES 'utf8'");
        echo "Conectado!</br></br>";
    }

    $listagem = mysqli_query($conexao, "SELECT * FROM aluno");

    while ($linha = mysqli_fetch_array($listagem)) {

        $alunoTemp = new Aluno($linha['nome'], $linha['endereco'], $linha['peso'], $linha['altura'], $linha['imc'], $linha['programa']);

        $sql = mysqli_query($conexao, "UPDATE aluno SET peso = peso WHERE nome=nome");  
}   

?>
    
asked by anonymous 09.03.2015 / 13:46

1 answer

1

In actual you are not changing the data with this query, it will update all the weight records by itself

$sql = mysqli_query($conexao, "UPDATE aluno SET peso = peso WHERE nome=nome"); 

You have to override the values you want to update as

$sql = mysqli_query($conexao, "UPDATE aluno SET peso = {$alunoTemp->peso} WHERE nome=\"{$alunoTemp->nome}\""); 
    
09.03.2015 / 13:58