UPDATE SET Does not work on Mariadb

Asked

Viewed 68 times

1

I have the following code on phpmyadmin, Mariadb

UPDATE bairro SET 
  NM_BAIRRO = 'Bairro'
 ,CD_CIDADE = 1
 ,CD_ZONA = 1
 WHERE CD_BAIRRO = 1

Someone can tell me, just because this command doesn’t work?

Command in php:

 public function update (Bairro $bairro){
        $this->connection =  null;
        $teste = false;
        $this->connection = new ConnectionFactory();
        //echo "Bairro: ".$bairro->getNmBairro();
        $this->connection->beginTransaction();
        try{
            $query = "UPDATE `bairro` SET 
                              `NM_BAIRRO`=:bairro
                             ,`CD_CIDADE`=:cidade
                             ,`CD_ZONA`=:zona 
                             WHERE `CD_BAIRRO`=:codigo";
            $stmt = $this->connection->prepare($query);
            $stmt->bindValue(":bairro", $bairro->getNmBairro(), PDO::PARAM_STR);
            $stmt->bindValue(":cidade",$bairro->getCidade()->getCdCidade(), PDO::PARAM_INT);
            $stmt->bindValue(":zona",$bairro->getZona()->getCdZona(), PDO::PARAM_INT);
            $stmt->bindValue(":codigo", $bairro->getCdBairro(), PDO::PARAM_INT);
            $stmt->execute();

            $this->connection->commit();

            $teste =  true;


            $this->connection =  null;
        }catch(PDOException $exception){
            echo "Erro: ".$exception->getMessage();
        }
        return $teste;
    }

The function I call

function change($id, $nome, $cidade, $zona){
    // echo "<script>alert('Adicionar'); </script>";

    require_once "../beans/Bairro.class.php";
    require_once "../controller/BairroController.class.php";
    require_once "../beans/Cidade.class.php";
    require_once "../beans/Zona.class.php";

    $bairro = new Bairro();
/*    echo "Codigo do bairro: ".$id."<br>";
    echo "Codigo do Cidade: ".$cidade."<br>";*/
    $bairro->setCdBairro($id);
    $bairro->setNmBairro($nome);
    $bairro->setCidade(new Cidade());
    $bairro->getCidade()->setCdCidade($cidade);
    $bairro->setZona(new Zona());
    $bairro->getZona()->setCdZona($zona);
    $bairroController = new BairroController();
    $teste = $bairroController->update($bairro);

    if($teste)
        echo json_encode(array('retorno' => 1));
    else
        echo json_encode(array('retorno' => 0));
}

my form

<?php
require_once "beans/Bairro.class.php";
require_once "controller/BairroController.class.php";

$id = $_POST['id'];

$bairroController = new BairroController();
$bairro = new Bairro();
$bairro = $bairroController->getBairro($id);



include "include/head.php"; ?>

<form method="post" id="form">
                    <input id="id" value="<?php echo $bairro->getCdBairro(); ?>" type="hidden">
                    <input id="acao" value="A" type="hidden">
                    <input id="id-cidade" value="<?php echo $bairro->getCidade()->getCdCidade();  ?>" type="hidden">
                    <input id="id-zona" value="<?php echo $bairro->getZona()->getCdZona();  ?>" type="hidden">
                    <div class="form-group col-xs-12 col-sm-12 col-md-10 col-lg-10">
                        <label for="bairro">Bairro</label>
                        <input id="bairro" class="form-control" required="" value="<?php echo $bairro->getNmBairro(); ?>"/>
                    </div>
                    <div class="row"></div>
                    <div class="form-group col-xs-12 col-sm-12 col-md-5 col-lg-5">
                        <label for="cidade">Cidade</label>
                        <select id="cidade" class="form-control" required="">
                            <option value="">Selecione</option>
                        </select>
                    </div>
                    <div class="col-lg-2 form-group" style="margin-top: 25px;">
                        <label></label>
                        <a href="#" title="Clique para atualizar a lista" class="btn btn-refresh"><i class="lnr lnr-sync"></i></a>
                    </div>
                    <div class="row"></div>
                    <div class="form-group col-xs-12 col-sm-12 col-md-5 col-lg-5">
                        <label for="zona">Zona</label>
                        <select id="zona" class="form-control" required="">
                            <option value="">Selecione</option>
                        </select>
                    </div>
                    <div class="col-lg-2 form-group" style="margin-top: 25px;">
                        <label></label>
                        <a href="#" title="Clique para atualizar a lista" class="btn btn-refresh1"><i class="lnr lnr-sync"></i></a>
                    </div>
                    <div class="row"></div>
                    <hr />
                    <div class="btn-group">
                        <button class="btn btn-success" onclick="salvar()">Salvar</button>
                        <a class="btn btn-warning btn-voltar" data-url="pais.php" onclick="return verifica('Tem certeza de que deseja cancelar a opera&ccedil;&atilde;o?');">Cancelar</a>
                    </div>

                </form>

my controller

 public function update (Bairro $bairro){
        require_once ("../model/BairroDAO.class.php");
        $bairroDao = new BairroDAO();
        $retorno = $bairroDao->update($bairro);
        return $retorno;
    }
  • 2

    Doesn’t work what? What’s wrong?

  • No error, gives success message only, but there is no change. Only 0 Line affected

  • Please answer the following questions: 1. Are you using this in PHP with mysqli or PDO? 2. If running via phpmyadmin or directly at the terminal does this problem occur? You are using "transaction"?

  • I am using 1: PDO, in phpadmyadmin I am using transaction

  • No phpmyadmin works

  • edited the question

  • just this method does not work and look that I followed the same pattern of all others

Show 2 more comments

1 answer

0

Look at you As I code the neighborhood zone is int the system was getting the name and at the time of giving the update not worked

It was simply because in the combobox I put the words together

<selec>
 <option selectedvalue="1">Leste</option
</select>

it’s the right thing to do

<selec>
 <option selected value="1">Leste</option
</select>

That’s why I wouldn’t save..

Thank you for trying to help me

Browser other questions tagged

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