Error: Notice: Undefined index || PHP || MYSQL ||

Asked

Viewed 196 times

0

Hi I’m developing a filter for an online store and I’m getting this error when I start the page,

inserir a descrição da imagem aqui inserir a descrição da imagem aqui

But when I click on the continue button of the form, everyone disappears except this: inserir a descrição da imagem aqui Here is the form code:

<div class="card-body"> 
<form method="post"> 
Referencia: 
<input type="text" placeholder="&#xF002; Search" name="referencia"style="font-family:Arial, FontAwesome"> 
<br><br> 
<div class="row"> 
Diametro do Aço (d): 
<div class="col-md-4"> 
<input type="text" name="d1" size="6" placeholder="min"> 
<input type="text" name="d2" size="6" placeholder="max"> 
</div> 
Comprimento total (L0): 
<div class="col-md-4"> 
<input type="text" name="comp1" size="6" placeholder="min"> 
<input type="text" name="comp2" size="6" placeholder="max"> 
</div> 
</div> 
<br> 
<div class="row"> 
Diametro Exterior (DE):
<div class="col-md-4"> 
<input type="text" name="de1" size="6" placeholder="min"> 
<input type="text" name="de2" size="6" placeholder="max"> 
</div> 
<br> 
</div>
<br> 
<div class="row"> 
<div class="form-group"> 
<label for="exampleFormControlSelect1">Ordenar por:</label> 
<select class="form-control" name="ordenar" id="ordenar"> 
<option value="referencia">Referencia</option> 
<option value="diametroaco">Diametro do Aço (d)</option> 
<option value="comprimentototal">Comprimento total (L0)</option> 
<option value="diametroexterior">Diametro Exterior (DE)</option> 
<option value="passo">Passo (P)</option> 
</select> 
</div> 
<div class="col-md-9"> 
<div align="right"> 
<img src="imagens/desenho33.png" align="right" height="250" width="350" > 
</div> 
</div> 
</div> 
<br> 
<div align="right"> 
<button type="reset" align="right" class="btn btn-primary">Reset</button> 
<button type="submit" align="right" class="btn btn-primary" value="submit">Continuar</button> 
</div> 
</form>

This is the code that is with the mistakes:

<?php 
if(isset($_POST)&&!empty($_POST)){ 
    if ($_POST["d1"]<>""){
$d1=$_POST["d1"];
    }else{
        $d1="0,200";
    }
    if ($_POST["d2"]<>""){
$d2=$_POST["d2"];
    }else{
        $d2="20";
    }
    if ($_POST["comp1"]<>""){
$comp1=$_POST["comp1"];
    }else{
        $comp1="1";
    } 
    if ($_POST["comp2"]<>""){
$comp2=$_POST["comp2"];
    }else{
        $comp2="10000";
    } 
    if ($_POST["de1"]<>""){
$de1=$_POST["de1"];
    }else{
        $de1="1";
    }
    if ($_POST["de2"]<>""){
$de2=$_POST["de2"];
    }else{
        $de2="200";
    }  
}
$ordenar=$_POST["ordenar"]; 

//if(isset($_POST["getproduct"])){ 
//$ordenar="nome"; 
//} 
switch($ordenar){ 
case "referencia": 
$ordenar_por="order by referencia"; 
break; 
case "diametroaco": 
$ordenar_por="order by diametroaco"; 
break; 
case "comprimentototal": 
$ordenar_por="order by comprimentototal"; 
break; 
case "diametroexterior": 
$ordenar_por="order by diametroexterior"; 
break; 
case "passo": 
$ordenar_por="order by passo"; 
break; 
} 

include ("db.php"); 
// $molcomp_query="SELECT * FROM stock_comp"; 
   //$molcomp_query="SELECT * FROM stock_comp $ordenar_por"; 
$molcomp_query="SELECT * FROM stock_comp WHERE (diametroaco BETWEEN '$d1' and '$d2') AND (comprimentototal BETWEEN '$comp1' AND '$comp2') AND (diametroexterior BETWEEN '$de1' AND '$de2') $ordenar_por"; 

$run_query = mysqli_query($con,$molcomp_query); 
         var_dump($run_query);
if (mysqli_num_rows($run_query) > 0){ 
while($row = mysqli_fetch_array($run_query)){ 
$id_mol_comp=$row['id_mol_comp']; 
$referencia=$row['referencia']; 
$diametroaco=$row['diametroaco']; 
$comprimentototal=$row['comprimentototal']; 
$diametroexterior=$row['diametroexterior']; 
$passo=$row['passo']; 
$preco=$row['preco']; 
echo" 
<div class='card mx-auto w-100'> 
<div class='card-body'> 
<div class='row'> 
<div class='col-md-1'>$referencia</div> 
<div class='col-md-2'>$diametroaco</div> 
<div class='col-md-2'>$comprimentototal</div> 
<div class='col-md-2'>$diametroexterior</div> 
<div class='col-md-1'>$passo</div> 
<div class='col-md-1'>$preco</div> 
<div class='col-md-1'><i style='font-size:30px' class='fa'>&#xf1c1;</i> <i style='font-size:30px' class='fa'>&#xf217;</i> </div> 
</div> 
</div> 
</div> 
<br>"; 
} 
}    
?>

  • 1

    When you start the page, there is no post so do not enter this if(isset($_POST)&&!empty($_POST)){ and gives the error of indefinite variables, this error probably causes $run_query fails to use it as a parameter for mysqli_num_rows causes another error. To be sure of a var_dump($_POST) before line 217 and see if the variables are there

  • yes, exactly this: the array appears empty: array (size=0) Empty

  • Already solved the problem? If not, remove the if or put everything inside it

2 answers

2

These errors (large part) are occurring, because the request method is not the same as in if:

if( isset($_POST) && !empty($_POST) ){ 

You are probably accessing the page directly and not by submitting a form.

Behold your code contains all the Dexes within the if which requires the requisition method to be POST and as soon as the method is GET the Dexes will all be indefinite.

Here you have two options:

1 - Pre-define the Dexes (before the if) and if the request was with the method POST would reset them, providing other corrections and validations that are required, or;

2 - Bring all the code that depends on these Dexes into the block of the if which is processed when the method is POST.

An example using option 2:

<?php
if( isset($_POST) && !empty($_POST) ){
    if ($_POST["d1"]<>""){
        $d1=$_POST["d1"];
    }else{
        $d1="0,200";
    }
    if ($_POST["d2"]<>""){
        $d2=$_POST["d2"];
    }else{
        $d2="20";
    }
    if ($_POST["comp1"]<>""){
        $comp1=$_POST["comp1"];
    }else{
        $comp1="1";
    } 
    if ($_POST["comp2"]<>""){
        $comp2=$_POST["comp2"];
    }else{
        $comp2="10000";
    } 
    if ($_POST["de1"]<>""){
        $de1=$_POST["de1"];
    }else{
        $de1="1";
    }
    if ($_POST["de2"]<>""){
        $de2=$_POST["de2"];
    }else{
        $de2="200";
    }
    $ordenar = $_POST["ordenar"];

    //if(isset($_POST["getproduct"])){ 
    //$ordenar="nome"; 
    //} 
    switch($ordenar){ 
        case "referencia": 
            $ordenar_por="order by referencia"; 
        break; 
        case "diametroaco": 
            $ordenar_por="order by diametroaco"; 
        break; 
        case "comprimentototal": 
            $ordenar_por="order by comprimentototal"; 
        break; 
        case "diametroexterior": 
            $ordenar_por="order by diametroexterior"; 
        break; 
        case "passo": 
            $ordenar_por="order by passo"; 
        break; 
    } 

    include ("db.php"); 
    // $molcomp_query="SELECT * FROM stock_comp"; 
       //$molcomp_query="SELECT * FROM stock_comp $ordenar_por"; 
    $molcomp_query="SELECT * FROM stock_comp WHERE (diametroaco BETWEEN '$d1' and '$d2') AND (comprimentototal BETWEEN '$comp1' AND '$comp2') AND (diametroexterior BETWEEN '$de1' AND '$de2') $ordenar_por"; 

    $run_query = mysqli_query($con,$molcomp_query); 
             var_dump($run_query);
    if (mysqli_num_rows($run_query) > 0){ 
        while($row = mysqli_fetch_array($run_query)){ 
            $id_mol_comp=$row['id_mol_comp']; 
            $referencia=$row['referencia']; 
            $diametroaco=$row['diametroaco']; 
            $comprimentototal=$row['comprimentototal']; 
            $diametroexterior=$row['diametroexterior']; 
            $passo=$row['passo']; 
            $preco=$row['preco']; 
            echo" 
            <div class='card mx-auto w-100'> 
            <div class='card-body'> 
            <div class='row'> 
            <div class='col-md-1'>$referencia</div> 
            <div class='col-md-2'>$diametroaco</div> 
            <div class='col-md-2'>$comprimentototal</div> 
            <div class='col-md-2'>$diametroexterior</div> 
            <div class='col-md-1'>$passo</div> 
            <div class='col-md-1'>$preco</div> 
            <div class='col-md-1'><i style='font-size:30px' class='fa'>&#xf1c1;</i> <i style='font-size:30px' class='fa'>&#xf217;</i> </div> 
            </div> 
            </div> 
            </div> 
            <br>"; 
        } 
    }   
}
?>

1


A lock problem in the wrong place, every code should be inside the if(isset($_POST)&&!empty($_POST)):

<?php
    if(isset($_POST)&&!empty($_POST)){
        if ($_POST["d1"]<>""){
            $d1=$_POST["d1"];
        }else{
            $d1="0,200";
        }
        if ($_POST["d2"]<>""){
            $d2=$_POST["d2"];
        }else{
            $d2="20";
        }
        if ($_POST["comp1"]<>""){
            $comp1=$_POST["comp1"];
        }else{
            $comp1="1";
        }
        if ($_POST["comp2"]<>""){
            $comp2=$_POST["comp2"];
        }else{
            $comp2="10000";
        }
        if ($_POST["de1"]<>""){
            $de1=$_POST["de1"];
        }else{
            $de1="1";
        }
        if ($_POST["de2"]<>""){
            $de2=$_POST["de2"];
        }else{
            $de2="200";
        } 

        $ordenar=$_POST["ordenar"];

        //if(isset($_POST["getproduct"])){
        //$ordenar="nome";
        //}
        switch($ordenar){
            case "referencia":
                $ordenar_por="ORDER BY referencia";
                break;
            case "diametroaco":
                $ordenar_por="ORDER BY diametroaco";
                break;
            case "comprimentototal":
                $ordenar_por="ORDER BY comprimentototal";
                break;
            case "diametroexterior":
                $ordenar_por="ORDER BY diametroexterior";
                break;
            case "passo":
                $ordenar_por="ORDER BY passo";
                break;
        }

        include ("db.php");
        // $molcomp_query="SELECT * FROM stock_comp";
           //$molcomp_query="SELECT * FROM stock_comp $ordenar_por";
        $molcomp_query="SELECT * FROM stock_comp WHERE (diametroaco BETWEEN '$d1' and '$d2') AND (comprimentototal BETWEEN '$comp1' AND '$comp2') AND (diametroexterior BETWEEN '$de1' AND '$de2') $ordenar_por";

        $run_query = mysqli_query($con,$molcomp_query);
        // var_dump($run_query);
        if (mysqli_num_rows($run_query) > 0){
            while($row = mysqli_fetch_array($run_query)){
                $id_mol_comp=$row['id_mol_comp'];
                $referencia=$row['referencia'];
                $diametroaco=$row['diametroaco'];
                $comprimentototal=$row['comprimentototal'];
                $diametroexterior=$row['diametroexterior'];
                $passo=$row['passo'];
                $preco=$row['preco'];
                echo
                "<div class='card mx-auto w-100'>
                    <div class='card-body'>
                            <div class='row'>
                            <div class='col-md-1'>$referencia</div>
                            <div class='col-md-2'>$diametroaco</div>
                            <div class='col-md-2'>$comprimentototal</div>
                            <div class='col-md-2'>$diametroexterior</div>
                            <div class='col-md-1'>$passo</div>
                            <div class='col-md-1'>$preco</div>
                            <div class='col-md-1'><i style='font-size:30px' class='fa'>&#xf1c1;</i> <i style='font-size:30px' class='fa'>&#xf217;</i> </div>
                        </div>
                    </div>
                </div>
                <br>";
           }
        }
    }
?>

Browser other questions tagged

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