How to update the database from a form using PDO?

Asked

Viewed 192 times

0

I have 2 files, the 1° is the form q will carry out the sending of the data and the 2° will process the data and update in the database, but the problem is q this is not happening, follow the code below... Note: Browser shows no error.

Form

<div id="<?php echo $mostra_produto['id_ovo'];?>editar" class="modal">
                <div class="modal-content">
                    <h4 class="center blue-text">
                        Editar Produto 
                        <p class="black-text smfont"><?php echo $mostra_produto['nome_ovo']; ?><br><span class="black-text smfont"><?php echo $mostra_produto['codigo_ovo']; ?></span></p>
                    </h4>
                    <form class="col s12 m12 l12 xl12" method="POST" action="processadores/processa_editar_ovo.php" enctype="multipart/form-data" autocomplete="off">
                        <div class="row">
                            <div class="input-field col s12 m12 l12 xl12">
                                <input name="titulo" type="text" id="titulo" autofocus />
                                <label for="titulo">Nome Do Ovo de Páscoa</label>
                            </div>
                            <div class="input-field col s12 m12 l12 xl12">
                                <textarea name="descricao" id="descricao" class="materialize-textarea" ></textarea>
                                <label for="descricao">Descrição</label>
                            </div>
                            <div class="input-field col s12 m12 l12 xl12">
                                <input name="preco" type="text" id="preco">
                                <label for="preco">Preço</label>
                            </div>
                            <div class="input-field col s12 m12 l12 xl12">
                                <input name="codigo" type="text" id="codigo" />
                                <label for="codigo">Codigo do Produto</label>
                            </div>
                            <div class="input-field col s12 m12 l12 xl12">
                                <p>
                                    <label>
                                    <input required="required" id="aceita" type="checkbox">
                                    <span class="bold red-text">Confirmo que todas as informações foram preenchidas corretamente.</span>
                                    </label>
                                </p>
                            </div>
                            <input class="btn-small green left" type="submit" name="editar" value="editar" />
                            <input class="modal-close btn-small red right" type="reset" name="cancelar" value="Cancelar" />
                        </div>
                    </form>
                </div>
            </div>

PDO

<?php
    include_once("../classes/conexao.class.php");
    Class Editar extends Conexao{

    private $pdo;
    private $id;
    private $query;

    public function __construct(){
        $this -> id = $_GET['id_ovo'];
        $this -> ExecutaUpdate($this->id);
    }

    private function MinhaConexao(){
        $this->pdo = parent::getConecta();
    }

    private function ExecutaUpdate($id){
        $this->MinhaConexao();

        if(isset($_POST['editar'])){
            $this->query = $this->pdo->prepare("UPDATE ovos SET nome_ovo = :titulo, descricao_ovo= :descricao, codigo_ovo = :codigo, preco_ovo = :preco WHERE id_ovo = :id_ovo");
            $this->query->execute();
        }
    }
}
?>

2 answers

0

Buddy Check the placeholders?

$this->query->bindParam(':titulo', 'algo');
$this->query->bindParam(':descricao', 'algo');
$this->query->bindParam(':codigo', 'algo');
$this->query->bindParam(':preco', 'algo');
$this->query->bindParam(':preco', 'id_ovo');

You have to pass the data to run. I can’t comment, I don’t have enough reputation. but that must be the problem.

  • Will this work ? if(isset($_POST['edit'])){ $this->query = $this->Pdo->prepare("UPDATE eggs SET name_ovo = '".$_POST['title']."', descricao_ovo = '".$_POST['Description']."', codigo_ovo = '".$_POST['codigo']."', preco_ovo = '".$_POST['preco']."' where '".$_POST['id_ovo']."' = '".$id."'"'); this$-->query->execute(); }

  • Well, I’ve never used it this way, but try, but I advise dealing with bindValue or bindParam

0

I changed the code to receive what comes from the post, but it is not yet possible to update

    <?php
include_once("../classes/conexao.class.php");
Class Editar extends Conexao{

    private $pdo;
    private $id;
    private $query;

    public function __construct(){

        $this -> id = $_GET['id_ovo'];
        $this -> ExecutaUpdate($this->id);
    }

    private function MinhaConexao(){
        $this->pdo = parent::getConecta();
    }

    private function ExecutaUpdate($id){

        $this->MinhaConexao();

        if(isset($_POST['editar'])){
            $this->query = $this->pdo->prepare("UPDATE ovos SET nome_ovo = '".$_POST['titulo']."', descricao_ovo = '".$_POST['descricao']."', codigo_ovo = '".$_POST['codigo']."', preco_ovo = '".$_POST['preco']."' WHERE '".$_POST['id_ovo']."' = '".$id."'");
            $this->query->execute();

        }
    }
}
?>

Browser other questions tagged

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