Hello
I have a form for a shoping cart that input even the value 0. How can i program php to not insert into table the products

$row['Denumire_produs']

where the quantyti=$cantitate is 0
Here is the form input

$query="select*from produse";
  
           ?>    <form action="cumpar.php"  method="post">
<td><input type="text" name="cantitate[]" value=" " size="1"/></td></tr>
<input type="hidden" name="Denumire_produs[]" value="<?php echo $row['Denumire_produs'];?>"/>
<input type="hidden" name="id_produs[]" value="<?php echo $row['id_produs'];?>"/>
<input type="hidden" name="Pret[]" value="<?php echo $row['Pret'];?>"/><?php ;}//Navigaton?>

AND HERE IS THE
QUERY FOR ARRAY

$numrows=mysql_num_rows($results);
     
        
    if ($numrows>0){
    $cantitate=($_POST['cantitate']);
    $denumire=($_POST['Denumire_produs']);
    $id_produs=($_POST['id_produs']);
    $pret=($_POST['Pret']);
    //extrage un array ---foarte tare bolean
    
 for($i=0;$i<$limit;$i++){

    $queryi="insert into livrari_luni
                (user_id,
                id_programare,
                id_produs,
                denumire_produs,
                Pret,
                cantitate
                )values(
                '". $userid . "',
                '". $id_prog . "',
                '". $id_produs[$i] ."',
                '". $denumire[$i] . "',
                 '". $pret[$i] . "',                
                '". $cantitate [$i] ."')";
                $resulti = mysql_query($queryi)
                or die (mysql_error);

thanks in advance

Test for the condition you want to eliminate in your loop.

for($i=0;$i<$limit;$i++){
    if(intval($cantitate[$i]) < 1) continue;   // skip to next entry in the array;
    $queryi="insert into livrari_luni
...

Test for the condition you want to eliminate in your loop.

for($i=0;$i<$limit;$i++){
    if(intval($cantitate[$i]) < 1) continue;   // skip to next entry in the array;
    $queryi="insert into livrari_luni
...

Thank you it working grate. :)

I use

if($cantitate[$i])> 1)

and it seem to be ok this way to;

This will skip values of 1 too, so either change it to < 1 or >= 1

Any variables with the values false/0/NULL/"" are considered to execute false in PHP

#execcute if $cantitate[$i] is not null
if($cantitate[$i]){
  //this part of the array is not NULL.....
}

Would also be fine.

Very good advice pritaes. It has been done.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.