PHP in_array $ _REQUEST

0

I'm checking the treatment of a web page . I have a series of valid IDs, existing in a table, stored in the variable $ misids; (Returns an array).

I want to check that the user is not "bad" and wants to access a non-existent id of the page.

GetID method:

static public function obtenerIDsCabanas(){
        $ejecucion = self::Conexion();
        $sql = "SELECT idcabana FROM cabanas";
        $registro = $ejecucion->query($sql);
        //Creamos un array para almacenar los IDs.
        $misids = array();
        //Recorremos el array y añadimos en él los ids mediante array_push.
        while($idcabana = $registro->fetch()){
            //Array asociativo: al array $misids le pasamos $idcabana.
            array_push($misids, $idcabana);
        }
        //Devuelve el array $misids (asociativo).
        return $misids;
    }

HTML Code:

<?php
    $misids = BD::obtenerIDsCabanas();
    if(in_array($_REQUEST["idcabana"], $misids)){
        echo "SI existe el ID.";
    }else{
        echo "No existe el ID.";
    }
?>

Questions:

1) Why do I always get "Does the ID not exist"?

    
asked by omaza1990 14.12.2017 в 09:55
source

1 answer

2

The thing is that you have the data in a multidimensional array, so you do not have to dig deeper. You should also check them with the password, since that is what you are looking for if I am not mistaken.

Try the following function.

$misids = array ( array ( 'idcabana' => 1, '0' => 1 ) ,array ( 'idcabana' => 2 ,'0' => 2 ), array ( 'idcabana' => 3, '0' => 3 ) );

if(in_multiarray(1, $misids,"idcabana")){
    echo "SI existe el ID.";
}else{
    echo "No existe el ID.";
}

function in_multiarray($elem, $array,$field)
{
    $top = sizeof($array) - 1;
    $bottom = 0;
    while($bottom <= $top)
    {
        if($array[$bottom][$field] == $elem)
            return true;
        else 
            if(is_array($array[$bottom][$field]))
                if(in_multiarray($elem, ($array[$bottom][$field])))
                    return true;

        $bottom++;
    }        
    return false;
}

Edited for your code:

$misids = BD::obtenerIDsCabanas();

if(in_multiarray($_REQUEST["idcabana"], $misids,"idcabana")){
    echo "SI existe el ID.";
}else{
    echo "No existe el ID.";
}

function in_multiarray($elem, $array,$field)
{
    $top = sizeof($array) - 1;
    $bottom = 0;
    while($bottom <= $top)
    {
        if($array[$bottom][$field] == $elem)
            return true;
        else 
            if(is_array($array[$bottom][$field]))
                if(in_multiarray($elem, ($array[$bottom][$field])))
                    return true;

        $bottom++;
    }        
    return false;
}
    
answered by 14.12.2017 / 10:03
source