I have a very strange error in my PHP code (if anyone can spot the problem, I'll try to be clear):
Beginning:
There is a variable that I have been passing through GET 2 links ago ( It is an ID to consult )... (I show it in green).
Then it happens that I get to a form... It is assumed that the form is sent to the same page, that is; The user writes the comment, clicks send and I want the comment to be saved below one another as shown below:
The problem is that when you hit "SEND" I get the following error:
Warning : Undefined array key "id" in D:\xampp\htdocs\zona_dis_nes\errores\reporte_erroes.php on line 25
I assume that what happens is that when the form button is pressed, the file: report_errors is reloaded and has a conflict with the variable "$GET_[id]" that it had been dragging since before, since when the page is reloaded it no longer It recognizes it and that's why I get the error of undefined array id ...
However I need that "$GET_[id]" to put it in my sql query which is like this:
$sql = "UPDATE contents SET CommentId = '$_GET[comment]' WHERE ID = ' $_GET[id] ' ";
So I ask: Does anyone know how I can avoid this error? (I have everything fine because in the "WHERE ID =" if I pass a number by hand if it updates the fields of the table, but obviously I need the $_GET[id] .
Here is my code:
include_once("../../_conexion_basededatos/database.php");
$_GET['id'];
echo "<div class='bg-white col-lg-7 col-sm-12'>";
/* Si se presiona ENVIAR recogo lo que se ENVIO*/
if (isset($_GET['comentario'])) {
$sql = "UPDATE contenidones SET IdComentarios = '$_GET[comentario]' WHERE ID = '$_GET[id]' ";
$resultado = mysqli_query($conn, $sql);
if ($resultado == true) {
echo "<div class='bg-white col-lg-7 col-sm-12'>";
echo "Tu comentario se a enviado, gracias";
echo "</div>";
} else {
echo "<div class='bg-white col-lg-7 col-sm-12'>";
echo "El comentario no pudo enviarse trata mas tarde";
echo "</div>";
}
}
/* ---*/
/* Aqui consulto una tabla COMENTARIOSNES (Esto es para probar, son los 3 comentarios que se ven en las imagenes*/
$sql2 = "SELECT * FROM comentarios_nes ";
$resultado2 = mysqli_query($conn, $sql2);
$datos = [];
while ($hileras = mysqli_fetch_assoc($resultado2)) {
$datos[] = $hileras;
}
/* ---*/
/* Esto es el FORMULARIO*/
echo "<form>";
echo "<div class='form-group'>";
echo "<input type='text' class='form-control' placeholder='Deja un comentario' name='comentario'> ";
echo "<small id='emailHelp' class='form-text text-muted'>Comentario de no mas de 150 caracteres</small>";
echo "</div>";
echo "<button type='submit' class='btn btn-primary' >Enviar</button>";
echo "</form>";
/* ---*/
/* Pinto los COMENTARIOS*/
foreach ($datos as $i) {
echo "<div class='bg-light mt-2' ><img src='/Imagenes/Iconos//icono_reporte.png' width=20 height=20> $i[Comentarios]</div>";
}
echo "</div>";
/* ---*/
If someone can contribute something thanks
First: the indices of your arrays must be enclosed in single or double quotes. Instead of, for example,
$_GET[comentario]
you should put$_GET['comentario']
Second, in your form you are not submitting the variable
id
, so obviously the script will not find it on subsequent submissions. Add a 'hidden' field in your form that contains the value of $_GET['id'] (always checking that the index exists):Another thing: you don't need to put the HTML inside statements
echo
. It becomes a nightmare when it comes to maintenance. In real situations the ideal is to use a template engine or at least write HTML interpolating PHP where necessary.