I would like to know if there is any function or method to evaluate when the user enters in the URL unwanted text or erroneous data that may "malfunction" the web. That is: if you delete any extension of the URL or insert unwanted characters, which instead of returning an error such as:
Notice: Undefined index: idcabana in C:\xampp\htdocs\daw\modificar.php on line 57
No existe el ID
We can have the option of displaying a customized screen for that error to make it more visible and usable (using W3C or HTML5 or CSS), as a test, with putting a message in the form of echo "" would be enough.
Example: If we delete from http://localhost/daw/modificar.php?idcabana=3
, the last 4-5 characters, and press Enter, we have the following URL: http://localhost/daw/modificar.php?idcaba
, which does not have any data and all the data that be passed to this page will be unrecognized, to avoid problems, put an echo ....
In this case, as expected "idcabana" and can not find it in the URL, it gives the error "Undefined index: idcabana".
HTML / PHP code:
<?php
require_once "Clases/BD.php";
require_once "Clases/Cabanas.php";
require_once "Clases/Accesorios.php";
require_once "conexion.php";
//Si existe la sesión "administrador"..., la guardamos en una variable.
if (isset($_SESSION['administrador'])){
$administrador = $_SESSION['administrador'];
}
//Si pulsamos el botón "Modificar"...
if(isset($_POST["modificar"])){
$idcabana = $_POST["idcabana"];
$nombre = $_POST["nombre"];
$capacidad = $_POST["capacidad"];
$descripcion = $_POST["descripcion"];
$precio = $_POST["precio"];
$array_accesorios = $_POST["accesorios"];
BD::modificarCabana($idcabana, $nombre, $capacidad, $descripcion, $precio);
BD::modificarAccesoriosPorCabana($idcabana, $array_accesorios);
header("Refresh:0; url=panel_administrador.php");
}
//Si pulsamos el botón "Atras"...
if(isset($_POST["atras"])){
header("Refresh:0; url=panel_administrador.php");
}
?>
<!DOCTYPE html>
<html lang="es">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Modificar cabaña</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="css/estilos_modificar.css">
</head>
<body>
<?php
function in_multiarray($elemento, $array, $campo){
$top = sizeof($array) - 1;
$bottom = 0;
while($bottom <= $top){
if($array[$bottom][$campo] == $elemento)
return true;
else
if(is_array($array[$bottom][$campo]))
if(in_multiarray($elemento, ($array[$bottom][$campo])))
return true;
$bottom++;
}
return false;
}
//Guardamos en una variable los ids de las cabañas.
$ids = BD::obtenerIDsCabanas();
//Si ese id no está en la tabla cabanas, mostramos un error.
if(!in_multiarray($_REQUEST["idcabana"], $ids, "idcabana")){
echo "No existe el ID";
}else{
?>
<div id="sesion_administrador">
<?php
if(isset($_SESSION['administrador'])){
echo "Bienvenido ".$administrador." ";
echo "<a href='salir_administrador.php?salir=1'>Salir</a>"; //GET
//_REQUEST = $_POST o $_GET
if(isset($_REQUEST["salir"])){
unset($_SESSION["administrador"]);
header("Refresh:0; url=iniciar_sesion_administrador.php");
}
}
?>
</div>
<br/><br/><br/>
<div id="mostrar_datos">
<!-- Modificar cabaña -->
<form action="modificar.php" name="modificar" id="modificar" method="POST">
<?php $objeto_cabana = BD::datosCabana($_REQUEST["idcabana"]); ?>
<label for="idcabana">ID: </label>
<?php $longitud_idcabana = strlen($objeto_cabana->getIdcabana()); ?>
<input type="text" size="<?php echo $longitud_idcabana; ?>" id="idcabana" name="idcabana" readonly="readonly" value="<?php echo $objeto_cabana->getIdcabana(); ?> "/>
<!-- Aqui va: <br/><br/> -->
<label for="nombre">Nombre: </label>
<?php $longitud_nombre = strlen($objeto_cabana->getNombre()); ?>
<input type="text" size="<?php echo $longitud_nombre; ?>" id="nombre" name="nombre" value="<?php echo $objeto_cabana->getNombre(); ?> "/>
<br/><br/>
<label for="capacidad">Capacidad: </label>
<?php
echo "<select name='capacidad'>";
for($i=1; $i<11; $i++){
if($i==$objeto_cabana->getCapacidad()){
echo "<option value='$i' selected>$i</option>";
}else{
echo "<option value='$i'>$i</option>";
}
}
echo "</select>";
?>
<br/><br/>
<label for="descripcion">Descripción: </label>
<?php $longitud_descripcion = strlen($objeto_cabana->getDescripcion()); ?>
<input type="text" size="<?php echo $longitud_descripcion; ?>" id="descripcion" name="descripcion" value="<?php echo $objeto_cabana->getDescripcion(); ?> "/>
<br/><br/>
<label for="precio">Precio: </label>
<?php $longitud_precio = strlen($objeto_cabana->getPrecio()); ?>
<input type="text" size="<?php echo $longitud_precio; ?>" id="precio" name="precio" onkeypress="return soloNumeros(event);" value="<?php echo $objeto_cabana->getPrecio(); ?> "/>
<br/><br/>
<div class="mostrar_accesorios" id="mostrar_accesorios">
<center>
<h3><b>Accesorios</b></h3><br/>
<?php
$objeto_accesorios = BD::obtenerAccesoriosPorCabana($_REQUEST["idcabana"]);
$todos_accesorios = BD::todosAccesorios();
foreach($todos_accesorios as $objeto){
$existe = false;
foreach($objeto_accesorios as $columna){
//Si el accesorio global está en el accesorio de la cabaña, true.
if($objeto->getIdaccesorio() == $columna->getIdaccesorio()){
$existe = true;
break;
}
}
if($existe){
echo "<div style='float:left; width:30%'><img src='imagenes/".$objeto->getDescripcion().".png' height='28px' width='34px' title='".$objeto->getDescripcion()."'/><br/><label for='accesorio".$objeto->getIdaccesorio()."'><input type='checkbox' value='".$objeto->getIdaccesorio()."' id='accesorio".$objeto->getIdaccesorio()."' name='accesorios[]' checked>".$objeto->getDescripcion()."</label></div>";
}else{
echo "<div style='float:left; width:30%'><img src='imagenes/".$objeto->getDescripcion().".png' height='28px' width='34px' title='".$objeto->getDescripcion()."'/><br/><label for='accesorio".$objeto->getIdaccesorio()."'><input type='checkbox' value='".$objeto->getIdaccesorio()."' id='accesorio".$objeto->getIdaccesorio()."' name='accesorios[]'>".$objeto->getDescripcion()."</label></div>";
}
$existe = false;
}
?>
<div style="clear:left"></div>
<br/><br/>
<form>
<input type="submit" value="Atrás" id="atras" name="atras" />
</form>
<!--<a href="javascript:history.back(-1);" title="Ir la página anterior">Atrás</a>-->
<input type="submit" value="Modificar" id="modificar" name="modificar" />
</center>
</div>
</form>
</div>
<?php
}
?>
</body>
</html>