Record form value and add to the database value

Asked

Viewed 55 times

1

I have a question, how do I add the entered value of the form to the data that is recorded inside the database? The structure is more or less this.

In the form action it takes me to the alterphp page.

$p = new Produto();

$id = addslashes($_POST['id']);
$nome = addslashes($_POST['nome']);
$quantidade = addslashes($_POST['quantidade']);
$descricao = addslashes($_POST['descricao']);

$p->alterarProduto($id, $nome, $quantidade, $descricao);

The function alterProduct is within Product.class.php and belongs to the Product() object, i search from the database the amount of products by the associated id in the page itself alters.php

global $pdo;
        $sql = "SELECT * FROM produtos WHERE id = '$id'";
        $sql = $pdo->prepare($sql);
        $stmt = $sql->execute();
        foreach ($sql as $row){
            $row['nome'];
            $row['quantidade'];
            $row['descricao'];
        }

The syntax would look something like this: $quantity = $quantity + $valueAtual, the problem that I’m not getting the values correctly and do the sum.

  • What values you’re not getting right?

  • If one of the answers is correct, I suggest you mark it as "accepted" so that your question can help other users as well

3 answers

0

I list the products as attached below Listar

when clicking edit it redirects me to this page looking for the name and quantity of products, but when I edit it only replaces the value, I want it to take the value 5 of the database and add with the value that the user type. Ex: 5 (BD value) + 3 (typed value) = 8. Editar produto

0

Never use the values directly in an sql, you should use the function bindParam() to predict SQL Injection.

To update a value added to another, you can do it directly in SQL:

    $quantidade = $_POST['quantidade'];
    $id = $_POST['id'];
    $query = $pdo->prepare('UPDATE produtos
        SET quantidade = (:quantidade + quantidade)
        WHERE id = :id');
    $query->bindParam(':quantidade', $quantidade, PDO::PARAM_INT);
    $query->bindParam(':id', $id, PDO::PARAM_INT);
    $query->execute();

However, don’t forget to validate the fields before doing the update.

  • Thanks for the help, that’s really what was missing, if it’s not a question of this syntax: PDO::PARAM_INT, is it only used in integer values? it makes some difference to stop using it, the question may be kind of stupid, but I started a short time to study about PDO and I was curious.

  • @Gilmarmh the third parameter is optional

  • Whenever you have questions, you can consult the official PHP documentation clicking here

-1

Hi @Gilmarmh You need to take the ID of the edited one and run an SQL with "update" in the Database. To be quite simple, take the new data and run an Update query with the new value.

The query would look something like:

$sql = "Update produtos set quantidade = ".$quantidade." WHERE id = '$id'";

And run it the same way you did the select.

It is worth consulting in: W3schools - PHP Mysql Update Data

Browser other questions tagged

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