Hello! I have a shopping cart with 2 images, one to enlarge and one to decrease the quantity. Everything works fine (it increases the quantity and updates the subtotal and tb the total. The same happens when I decrease.) I have a button to remove each item, which works perfectly. The problem is that the amount can not be less than 1, as it decreases (0, -1, -2 ....) and updates the values with negative value. Anyway, I need the quantity not less than 1 or, if it is, that the item be removed. Thank you in advance for being able to help me.
Follow the code:
<?php
require ("conexao.php");
if(count($_SESSION['valida_carrinho']) == 0){
echo "
<table style='background-color:#EEEEEE; width:94%; height:40px; text-align:center; margin-top:10px; margin-left:40px; font-size:15px;'>
<tr>
<td><strong>Não há produtos no carrinho.</strong></td>
</tr>
</table>";
}else{
$total = 0;
foreach($_SESSION['valida_carrinho'] as $id => $quantidade){
$sql = "SELECT * FROM produtos WHERE id = '$id'";
$query = mysqli_query ($conn, $sql)or die (mysqli_error());
$line = mysqli_fetch_assoc($query);
$imagem = $line['imagem'];
$nome = $line['nome'];
$preco = $line['preco'];
$subtotal = $line['preco'] * $quantidade;
$total = $total += $subtotal;
$_SESSION['total'] = $total;
echo'
<table>
<tr><td><img src ="imagens/'.$line['imagem'].'"/></td>
<tr><td>'.$nome.'</td></tr>
<tr><td>R$ '.number_format($preco, 2, ',', '.').'</td></tr>
<tr>
<td><a href="?acao=sub&id='.$id.'"><img src = "imagens/negativo.png"></a>
<input id= "product" type = "text" name = "product['.$id.']" value = "'.$quantidade.'"style="text-align:center; width:25px; height:25px;"/></a>
<a href="?acao=add&id='.$id.'"><img src = "imagens/positivo.png"></a></td>
</tr>
<tr><td><a href="?acao=del&id='.$id.'">Remover</a></td>
</table>';
}