UPDATE of information in the database with PHP using PDO

Asked

Viewed 195 times

0

I’m trying to do an update after sending the data through the form but I can’t, it just doesn’t update anything. I check the data by $_GET['id'] and the $_POST is passing values and everything is ok.

Code:

// Verificando se os campos não estão vazios.
if (empty($_POST['nome']) || empty($_POST['quant']) || empty($_POST['valor'])) {
    $msg = "Nenhum campo pode ficar vazio.";
} else {

    // SQL.
    $sql = "UPDATE produto SET nome_produto = :nome, quant_produto = :quant, valor_produto = :valor WHERE id_produto = :id"; 

    // Estabelecendo conexão com o bando e passando os dados com prepare() e via bindValue().
    $stmt = $pdo->prepare($sql);
    $stmt -> bindValue(":id", $_GET['id']);
    $stmt -> bindValue(":nome", $_POST['nome']);
    $stmt -> bindValue(":quant", $_POST['quant']);
    $stmt -> bindValue(":valor", $_POST['valor']);

    // Verificando se deu tudo certo durante a execução.
    if ($stmt->execute()) {
        header('Location: ../index.php');
    } else {
        $msg = "Erro ao editar produto.";
    }

}

I realized that by placing/setting values directly in bindValue() works, example:

    // SQL.
    $sql = "UPDATE produto SET nome_produto = :nome, quant_produto = :quant, valor_produto = :valor WHERE id_produto = :id"; 

    // Estabelecendo conexão com o bando e passando os dados com prepare() e via bindValue().
    $stmt = $pdo->prepare($sql);
    $stmt -> bindValue(":id", 10);
    $stmt -> bindValue(":nome", "Alguma coisa");
    $stmt -> bindValue(":quant", 2000);
    $stmt -> bindValue(":valor", 3.5);

    // Verificando se deu tudo certo durante a execução.
    if ($stmt->execute()) {
        header('Location: ../index.php');
    } else {
        $msg = "Erro ao editar produto.";
    }

I tried everything, with bindParam(), I checked if I was wrong on something, tried other things but came up with nothing. Only way it works is this one shown above.

  • You have already tried to pass the specific data type in the 3 parameter?

  • Putting the PDO::PARAM_INT... If that’s what I already tried. I don’t know what’s going on, worse than no mistake, that’s fuck :(

  • have you tried to associate variables before and validate content (the variable value)? for example $nome = $_POST['nome'] and $stmt -> bindValue(":nome", $nome);?

  • I don’t think so, but I’ll try.

  • I was able to verify what is happening, every time I put $_GET['id'] or a variable receiving it as parameter in bindValue() does not work the update. Now if I manually set the id number, example: 30, it performs the update... :s I believe that when clicking the edit button when I get the id via get, reset it then does not update the corresponding id. What I do now to get this value without being reset when I click edit and refresh the page.

  • I did some things that I solved the problem in a way, I don’t know if it was a gambiarra or a side option, kkkk. Thank you very much!

Show 1 more comment
No answers

Browser other questions tagged

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