guys, I have a form with 'n' quantities of inputs, all with the same structure:
echo "<input name='quantidade[]' type='number'>";
When I finish the form and send it to the next page, it arrives to me in array, and I use this array to save the data in a MySQL Database, I use this method to save in the database:
for( $i=0; $i<count($_POST['item']); $i++ ) {
// insere no banco
$PDO = db_connect();
$sql = "INSERT INTO carrinho(quantidade) VALUES ('".$_POST['quantidade'][$i]."')";
$stmt = $PDO->prepare( $sql );
$stmt->bindParam( ':quantidade', $_POST['quantidade'][$i] );
if ($stmt->execute())
{
header('Location: index.php');
}
else
{
echo "Erro ao cadastrar";
print_r($stmt->errorInfo());
}
}
It saves everything, and the data that has no value in the INPUT it saves with value 0, I wanted to know how do I not save the arrays with value "0".