I need to save a numeric variable, which generates random numbers but whenever I try to save it to my database I get "Undefined variable"
Code that generates the random numbers:
<?php
$num_lower = 1000;
$num_hight = 9999;
$num_solicitud = mt_rand($num_lower, $num_hight);
echo $num_solicitud;
?>
Code with which I am trying to save to my database:
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$num_solicitud;
$name = $_POST['name_M'];
$notas = $_POST['notes'];
$insertar = $conexion->query("INSERT INTO solicitudes (name, num_solicitud, notas) VALUES ('$name', '$num_solicitud', '$notas')");
if ($insertar) {
echo "<script>alert('Solicitud registrada exitosamente')</script>";
} else{
echo '<script>("Ups...Algo No va bien")</script>';
}
}
?>
This is what I have before html:
<?php
include 'config.php';
session_start();
if (!isset($_SESSION['nuip'])) {
header("Location: pagina_principal.php");
}
ini_set('display_errors', '1');
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$num_solicitud = $_POST['num_solicitud'];
$notas = $_POST['notes'];
$insertar = $conexion->query("INSERT INTO examenes
(num_solicitud, notas) VALUES ('$num_solicitud', '$notas')");
if ($insertar) {
echo "<script>alert('Examen registrado exitosamente')
</script>";
} else{
echo '<script>("Ups...Algo No va bien")</script>';
}
}
?>
My html code:
<div class="container" id="container">
<form method="post"><div class="titulo">
<h1 class="tituloPrincipal">Bioreferencia</h1>
<h6 class="parrafo_num_orden">Solicitud número: <?php
$num_lower = 1000;
$num_hight = 9999;
$num_solicitud = mt_rand($num_lower, $num_hight);
?><input type="hidden" name="num_solicitud" value="<?php echo $num_solicitud; ?>"></h6>
</div></form>
Before starting the HTML I have the PHP code where I am starting a session and also where I store everything sent by the form to my database.
The problem you have is that on the one hand you are defining the $request_num while you are displaying your HTML, but when they fill out the form you are not submitting it with the other POST data, and therefore you cannot access it.
The solution is to leave the code as you had it at the beginning of your question, but making the following changes:
For this:
and leave it like this:
Well, that
<form></form>
one here doesn't make any sense, it was superfluous, and the input field that I asked you for was not in this form, but in the next one.For this:
With this you will be adding a hidden field (hidden) with the value of the variable
$num_solicitud
and with the name num_request that in step 1 we will recover to be able to use it in the query.Try and tell me.
Previous answer (OBSOLETE: when I didn't have all the data)
It is not very well understood what you do with $num_request. I guess to fix it you have to put everything in the same script because I don't know if you do it in two right now.
That is, the solution would be this: