Update database by SQL PHP

Asked

Viewed 110 times

0

Staff I am 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");  
}   

?>
  • 2

    The difficulty is an error or unexpected result? could describe better.

  • 1

    If you’re making a mistake, edit your question and add this information.

  • Do you want to pass the values of the student object in the update? pass the respective properties, use simple quotes for text field values, or use Prepared statements.

  • change weight = weight by weight = {$alunoTemp->weight} and do the same in Where, and if I were you would update by id instead of name

1 answer

1

In fact 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 replace the values you want to update as

$sql = mysqli_query($conexao, "UPDATE aluno SET peso = {$alunoTemp->peso} WHERE nome=\"{$alunoTemp->nome}\""); 
  • Depending on the configuration double quotes " has the effect of \ `

  • I made the correct correction, but when I print, only the information of the first student comes out, follows the end of the coding: $sql = mysqli_query($connected, "UPDATE student SET weight = {$alunoTemp->weight} WHERE name="{$alunoTemp->name}"); echo "The student $alunoTemp->name, which resides in $alunoTemp->address with the weight of $alunoTemp->kg weight and height $alunoTemp->m height, has the BMI = $alunoTemp->imc should make a program $alunoTemp->program</br>";

Browser other questions tagged

You are not signed in. Login or sign up in order to post.