I want to thank everyone for their time in advance.
When I try to insert a series of data in a table called "comments", the affirmative answer of the mysqli_query that I placed inside the "if" is not given. I have checked in detail the database to verify that the names of the fields in which I want to insert the variables are the same as those I call in my SQL code; I also verified that all the variables have a defined value; and finally I checked the SQL statement I made to verify that it is syntactically correct. However, the error of the "else" (specifically the error "error2") continues to appear.
Do you see what the problem may be in the code that I leave here below?
Code:
<?php
if(!isset($_SESSION)){
session_start();
}
require_once("conexion.php");
$id_usuario = $_SESSION["id"];
$nombre = $_SESSION["user"];
$comentario = utf8_encode($_POST["comentario"]);
$fecha = utf8_decode(date("M jS\, Y (H:i a \U\T\C)" ,time()));
$articulo = utf8_encode($_POST["titulo"]);
$respuesta = false;
$identificadorUnico = mt_rand() . $fecha;
$img;
$query1 = "SELECT * FROM users WHERE id = '$id_usuario'";
if($resp1 = mysqli_query($conexion, $query1)){
$row = mysqli_fetch_assoc($resp1);
$img = "url('../" . $row["img"] . "')";
$query2 = "INSERT INTO comentarios (id_usuario, nombre, img, comentario, fecha, articulo, respuesta, identificadorUnico) VALUES ('$id_usuario', '$nombre', '$img', '$comentario', '$fecha', '$articulo', '$respuesta', '$identificadorUnico')";
if($resp2 = mysqli_query($conexion, $query2)){
echo '
<article class="ContenedorComentario">
<h1 class="fechaDelComentario">' . $fecha . '</h1>
<section style="background-image:' . $img . ';" class="imgUserComentario">
<article class="contenedorNombreDeUsuario">
<h1 class="nombreDeUsuarioQueComento">'
. $nombre .
'</h1>
</article>
</section>
<h1 class="comentario">
<div id="trianguloComentario"></div>
' . $comentario . '
</h1>
<h1 class="responderComentario" onclick="responderComentario("' . $identificadorUnico . '")">Reply</h1>
</article>
';
}else{
echo "error2";
}
}else{
echo "error1";
}
mysqli_close($conexion);
?>
Here is a screenshot of the table in the database:
This is the $ query2 taking shape on the page (request of someone who commented on the post):
Thank you very much everyone!