Php - update + or - in the field

Asked

Viewed 33 times

-4

I have created a db with

             Nome produto |  stock

                 Rato                     3

My goal is to. when you enter the number in the input it see if it is positive(1) or negative(-1) and then update the gender "stock = stock + 1, or stock = stock -1.

How do I do this using input?..

  • You see them annoy.. Nothing they said is a possible help.. i want php to check if the input number is negative or positive... and then input the update correctly

  • 1

    I did not understand your revolt. You asked that the update be in stock form = stock + 1 and it is exactly the way it was explained in the question quoted. Did you open the link? About identifying positive and negative will be unnecessary since if you add a negative value is the same as subtract. Basic mathematics. If, after studying the material cited, you still have any doubt, I ask you to edit your question and clarify better what you still need to complete.

1 answer

0

Come on João! I believe that what you are looking for is very simple, I will try to be as didactic as possible.

1° Part (Input)

<form type="post" action="NOMEDAPAGINA.php"> //POST pega o valor e ACTION manda pra pagina do PHP
<input name="n1" type="number"> //input
</form>

2° Part (Taking from the Values)

$n1 = isset($_POST['n1'])?$_POST['n1']:0; // $n1 = SE TIVER VALOR "do input n1" PEGUE "valor input n1" SENÃO "valor vai ser 0"; 
$stock = mysqli_query($conexao, "SELECT nome_do_campo FROM nome_da_tabela WHERE cd_do_registro"); //Pega o valor do stock no banco de dados
$array = mysqli_fetch_assoc($stock); //joga o valor para uma variavel

3° Part (Condition)

if ($n1 < 0){ //Se for negativo 
$total = $array['stock'] - 1; //Retira -1 do stock
mysqli_query($Conexao, "UPDATE Nome_da_tabela SET Nome_do_Campo ='$total' WHERE cd_do_registro"); //Muda valor no banco
} else { //Se for positivo
$total = $array['stock'] + 1; //soma +1 do stock
mysqli_query($Conexao, "UPDATE Nome_da_tabela SET Nome_do_Campo ='$total' WHERE cd_do_registro"); //Muda valor no banco
}

I hope I helped ! Good luck.

Browser other questions tagged

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