send only completed PHP ARRAY data

Asked

Viewed 25 times

1

Good night, you guys! table is already almost ready only need take the value that is selected in the quantity field is less than 0 not send to the database so send the value that I put in the field $amount that is relevant more than 0 my help please...

html table fields.

<tr>
            <td><input type="text" readonly class="form-control" name="codigo[]" id="inlineFormInputGroup" value="091822"></td>
            <td><input type="text" readonly class="form-control" name="material[]" id="inlineFormInputGroup" value="TOMATE GRAPE 180"></td>
            <td><input type="number" class="form-control" name="quantidade[]" id="inlineFormInputGroup" placeholder="Valor" ></td>
     
</tr>
<tr>
            <td><input type="text" readonly class="form-control" name="codigo[]" id="inlineFormInputGroup" value="091822"></td>
            <td><input type="text" readonly class="form-control" name="material[]" id="inlineFormInputGroup" value="TOMATE GRAPE 180"></td>
            <td><input type="number" class="form-control" name="quantidade[]" id="inlineFormInputGroup" placeholder="Valor" ></td>
     
</tr>

CODE IN PHP

<body>
<?php 

$material = $_POST["material"];
$codigo = $_POST["codigo"];
$quantidade= $_POST["quantidade"];  PRECISO QUE  ENVIE ESSE VALOR SE ELE FOR PREENCHIDO MAIOR QUE 0.
$total_codigo = count($codigo);

$conn = mysqli_connect($servidor,$dbusuario,$dbsenha,$dbname);
mysqli_select_db($conn,'$dbname');

for($i = 0; $i  <  $total_codigo;  $i++){
   $sql = "INSERT INTO tblavitaperdas (material,codigo,quantidade) VALUES 
           ('$material[$i]','$codigo[$i]','$quantidade[$i]')";
 
 if (mysqli_query($conn, $sql)) {
    echo "<script>alert('Salvei seus dados !'); window.location = 'perdaslavita.php';</script>";
    
    
    }else{
     echo "Deu errro: " . $sql . "<br>" . mysqli_error($conn);
    }
  
    
    }



mysqli_close($conn);
?>
</body>
</html>
  • cannot perform a validation before doing the Insert (if ($quantidade > 0)?

  • worst I’m not getting to make this validation I’ve tried several ways.

1 answer

0

I could do like this for example:

$materialarray = $_POST["material"];
$codigoarray = $_POST["codigo"];
$quantidadearray= $_POST["quantidade"];

for ($i = 0; $i < count($codigoarray); $i++) {
   $material   = $materialarray[$i];
   $codigo     = $codigoarray[$i];
   $quantidade = $quantidadearray[$i];

   if ($quantidade > 0) {
       $sql = "INSERT INTO tblavitaperdas (material,codigo,quantidade) VALUES 
           ('$material','$codigo','$quantidade')";
   }
}

Browser other questions tagged

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