I realize a system that basically is a logs of loan equipment. What I intend you to do is that users who need a computer to loan make a record, which is necessary to validate if the equipment is available or not for its use, and only appear in SELECT
the equipment available .
My main table prestamo
is composed of foreign keys. I have fields type NULL
since those fields have to see, or rather, they are modified or filled by the administrator when the user returns the equipment (in another loan edition form).
Here the structure of my table prestamo
:
id_prestamo int(15) Primaria
id_etiqueta int(15) FK UNIQUE
fecha1 date
hora1 time
id_usuario int(15) FK
id_departamento int(15) FK
id_entrega int(15) FK
fecha2 date NULL
hora2 time NULL
id_ingeniero int(15) FK NULL
The validation that I need you to perform is on the field id_etiqueta
and on id_ entrega
, which is in the table prestamo
(mentioned above).
Structure table tag
id_etiqueta int(15) Primaria
descripcion varchar(50)
Structure table entrega
:
id_entrega int(15) Primaria
descripcion varchar(50)
An example is:
If my field id_etiqueta = 1
( 1
equals LAP-01
) compare it with id_entrega = 1
( 1
equals OCUPADO
).
Then NO will show that label in SELECT
of id_etiqueta
so that users can not see it.
This is so that they can not occupy it, until you change id_entrega = 2
( 2
equals Disponible
), this is when it will appear again in the SELECT
tag.
PHP Code:
<?php
require("connect_db.php");
if (
false === empty($_POST['id_etiqueta'])
&& false === empty($_POST['fecha1'])
&& false === empty($_POST['hora1'])
&& false === empty($_POST['id_usuario'])
&& false === empty($_POST['id_departamento'])
) {
/* Preparamos la consulta usando ? para insertar los valores */
$stmt = $mysqli->prepare("
INSERT INTO prestamo (
id_etiqueta,
fecha1,
hora1,
id_usuario,
id_departamento
) VALUES (
?,
?,
?,
?,
?
)
");
/* Comprobamos si hubo problemas con la preparación de la consulta */
if ($stmt === false) {
die($mysqli->error);
}
/* Asignamos las variables, no es necesario crear variables intermedias */
$stmt->bind_param('sssss',
$_POST['id_etiqueta'],
$_POST['fecha1'],
$_POST['hora1'],
$_POST['id_usuario'],
$_POST['id_departamento']
);
/* Comprobamos si se pudo realizar la consulta */
if($stmt->execute() === true) {
$mensaje = "Registro agregado correctamente";
} else {
/* Comprobamos si hubo duplicidad de registro (clave duplicada) */
if ($stmt->errno === 1062) {
$mensaje = "Error:\nRegistro duplicado";
} else {
$mensaje = "Error al ingresar:\n" . $stmt->error;
}
}
/* Enviamos al navegador de manera segura el mensaje de texto */
echo '<script>alert(', json_encode($mensaje), ')</script>';
}
?>
Select id_etiqueta
Here I insert my database by ID, since it is a foreign key and I show it by description to the user:
<div class="form-group">
<label for="id_etiqueta" class="col-sm-2 control-label">Equipo Disponible:</label>
<?php
require("connect_db.php");
$query = "SELECT DISTINCT e.id_etiqueta, e.descripcion
FROM etiqueta e
INNER JOIN prestamo p on p.id_etiqueta=e.id_etiqueta
WHERE p.id_entrega!=1";
$res = $mysqli->query($query);
$option = '';
while ($row = $res->fetch_assoc()){
$option.="<option value=\"$row[id_etiqueta]\">$row[descripcion] </option>";
}
?>
<div class="col-sm-10">
<select type="text" class="form-control" id="id_etiqueta" name="id_etiqueta" placeholder="Etiqueta" required>
<option value="-">Selecciona Equipo Disponible</option>
<?php echo $option; ?>
</select></div>