Hoping that everyone is doing well, I have had an inconvenience (Perhaps as a newbie) when I wanted to save information in my database from php to MySQL.
Describing the workflow of my little website.
Previously, I requested help on this page to achieve that based on a "trigger" click that would update a specific div where the information would be. I've already done that, thank you very much. Now, in a certain update of the div, I get to one where it consists of seeing some entries already made and a section to be able to make a comment, a more specific form. Basically there is only a textarea to insert the comment itself, the other data is collected from the current session and others (we will emphasize later), and a button which, in theory, is supposed to trigger a certain php code to insert the record in question.
What have I tried?
Based on the extensive php and mysql video tutorials we find all over the internet, the methodology is almost the same itself, but oddly enough, it doesn't work for me.
What I currently have in my code is the following:
<form action="" class="ejemplo" method="POST">
<!-- <input type="text" name="respuesta"> -->
<input type="submit" name="guardar" value="Enviar respuesta">
<textarea name="respuesta" name="respuesta"></textarea>
<?php
//if (isset($_POST['guardar'])) {
$textor = mysqli_real_escape_string($con, $_POST['respuesta']);
//if(empty($textor)){
echo "<font>Introduzca algo!</font>";
//}else{
$fechareg = date('d-m-Y H:i:s');
echo $fechareg;
echo $textor;
$sentencia = "INSERT INTO respuestas(n_control, id_pregunta, contenido, fecha) VALUES ('{$_SESSION['usuario']}','{$id}', '{$textor}', '{$fechareg}')";
$result = mysqli_query($con, $sentencia);
//}
//}
?>
</form>
Before I clarify, you will see the if with the isset commented, since, if I ignore that validation, it DOES execute the query, but wrongly done, the user respects it, the id also, the content is blank, and the date as 0000 -00-00 00:00:00. The strange thing is also that in the echo of the variables where the date is supposed to be, it comes out fine, it is printed correctly, the text is still blank.
Now when that piece of code is executed, it prints:
Warning: Undefined array key "respuesta" in C:\xampp\htdocs\pag\ver_datos.php on line 80
As if the "answer" did not exist, but it is well raised in the form in its name. If I leave the if of isset, it just refreshes the page and does absolutely nothing, not even insert. You may also admire that it tries a simple input text and likewise it doesn't recognize it, it doesn't retrieve your text.
I heard that it can also be done through ajax, but I wouldn't know how to use it, I'm a newbie here. In the same way, do it in a separate php, but I can't find how to pass the variables such as the id.
I add the code of how the id and the user are obtained (That's right, based on what was previously selected, I get the id with post):
<?php
$id = $_POST['id'];
$titulo = $_POST['titulo'];
include("conexion.php");
$con=conectar();
session_start();
?>
If it is useful, I will tell you that above I receive other comments from other users. That works correctly.
Why is it that it doesn't even recognize the name of the input, and the date prints it fine but when sending it to the database it puts it in 0's? What am I doing wrong?