check difference between values with php

Asked

Viewed 190 times

-3

Needed help to check if the quantity chosen by the customer is different from the existing stock

Code I am using

$con=mysqli_connect('localhost', 'root', '', 'pap') or die (mysqli_error ());

$strSQL1 = "SELECT 'QuantidadeProduto' FROM `produtos` WHERE `Id_Produto`=1";

$rs1 = mysqli_query($con,$strSQL1);



  if(isset($_POST['verifica1'])){
    $quantiade = $_POST['quantidade'];
  }

  if($quantiade>$rs1){
    echo ("inferiror");
  }
  elseif ($quantiade<$rs1){
    echo("Falta stock");
  }

and is giving me the following mistakes

Warning: Use of Undefined Constant quantiade - assumed 'quantiade' (this will throw an Error in a Future version of PHP) in C: xampp htdocs Site pagina_produto.php on line 635

Warning: Use of Undefined Constant quantiade - assumed 'quantiade' (this will throw an Error in a Future version of PHP) in C: xampp htdocs Site pagina_produto.php on line 638

  • you lack the $ behind the quantity

  • gives error in the same why tried without. gives the following error Notice: Undefined variable: quantiade in C: xampp htdocs Site pagina_produto.php on line 635

  • in the comic you have the field "quantity" correct ?

  • that quantity field is the number of items that is indicated by a select. In the database the field is called Quantityproduct

  • Your error may be because the variable $_POST['quantidade'] does not return anything, in this case its variable $quantiade nor will be declared.

  • but it returns the right value, ie the variable is declared

  • if you echo in $rs1, it brings what?

  • Modifies $quantity = $_POST['quantity']; for $quantity = $_POST['Quantityproduct'];

  • @Rafa gives error in the same -> Notice: Undefined index: Quantityproduct in C: xampp htdocs Site pagina_product.php on line 634 1Falta stockstdClass Object ( [name] => [def] => [db] => [Catalog] => def [max_length] => 17 [length] => 17 [charsetnr] => 8 [flags] => 1 [type] => 253 [decimals] => 31 )

  • Check my answer in this case, use PDO, you can use this example to compare https://answall.com/questions/305092/contar-texto-equal-do-banco-data

  • People error is not because quantity is misspelled??? "Quanauntof"

  • No @Leandroangelo you name the variable you want

Show 7 more comments

2 answers

2

The error, as already said of the lack of $, is that you are not fetch the query result:

$con=mysqli_connect('localhost', 'root', '', 'pap') or die (mysqli_error ());

$strSQL1 = "SELECT 'QuantidadeProduto' FROM `produtos` WHERE `Id_Produto`=1";

$rs1 = mysqli_query($con,$strSQL1);

    while ($row = mysqli_fetch_row($rs1)) {
        printf ("%s (%s)\n", $row[0]);
    }
  • has already been put the $ in the code and the example above too

  • and you tested with the fetch I placed?

  • gives the following error --> Notice: Undefined offset: 1 in C: xampp htdocs Site pagina_produto.php on line 630 Quantidadeproduct () . On line 630 is the printf

  • I corrected the answer.

  • gives the following error : Warning: printf(): Too few Arguments in C: xampp htdocs Site pagina_produto.php on line 630

  • and after clicking to confirm gives this : Warning: printf(): Too few Arguments in C: xampp htdocs Site pagina_produto.php on line 630 stdClass Object ( [name] => [def] => [db] => [Catalog] => def [max_length] => 17 [length] => 17 [charsetnr] => 8 [flags] => 1 [type] => 253 [decimals] => 31 )

Show 1 more comment

0

Two things are wrong or incomplete in your code.

The first is that $ is missing before the quantity variable in two lines. And the second is that this way does not guarantee that the variable $quantiade will always be declared.

You can correct as follows:

if(isset($_POST['verifica1'])){
    $quantiade = $_POST['quantidade'];
}else{
    $quantiade = 0; // um exemplo, você pode colocar outro valor como padrão
}

if($quantiade>$rs1){
    echo ("inferiror");
}
elseif ($quantiade<$rs1){
    echo("Falta stock");
}
  • as commented above the "$" had been taken but even with it gives the error. Thank you for the alert for checking :)

  • I understand, but you need to fix it too, besides Else in isset post to work :)

  • yes already corrected , but still gives error

  • What’s the error message now?

  • Notice: Undefined index: quantidade in C: xampp htdocs Site pagina_produto.php on line 636 - check to see what you get, after clicking the button works normally Notice: Object of class mysqli_result could not be converted to int in C:xampp htdocs Site pagina_produto.php on line 639 Notice: Object of class mysqli_result could not be converted to int in C: xampp htdocs Site pagina_produto.php on line 643

  • This indicates that the $_POST['quantidade'] does not exist, the second error indicates that you cannot make comparisons with the variable $rs1 as if it were an integer, because it is not. Give it a print_r to see the format of the variable, it is probably an object or array.

Show 2 more comments

Browser other questions tagged

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