SQL does not host data

1

For the following problem I have as an object practice to send a form to MySQL; my code, which is by the PDO method, does not mark errors. So when I fill in the fields, when I run the code, the machine tells me that the insertion of data was correct, but when I look at the database, it does not contain anything, no matter how much I actulize. I repeat the procedure with different data but it is the same. So my question is: What could be failing?

This is the connection to the server.

<?php 
$PDO = new PDO( 
'mysql:host=localhost;dbname=db_mensajes;charset=UTF8','root','' );
try { 
echo "mensaje enviado";
$sql=$PDO->prepare("INSERT INTO usuarios( nombre, clave, mensaje) VALUES ( 
:nombre, :clave, :mensaje)");
$sql->bindParam(':nombre',$_POST['nombre']);
$sql->bindParam(':clave',$_POST['clave']);
$sql->bindParam(':mensaje',$_POST['mensaje']);
$sql->execute();

 }catch(PDOexception $e) {
 echo "mensaje no enviado:".$e ->GetMessaje();
 }
 ?> 

This is the form.

<form method="POST" action="insertar.php">
    <h1>Envio de mensaje</h1>
    <label>Nombre:</label>
    <input type="text" required="" placeholder="escribe tu nombre"><br><br>
    <label>Clave:</label>
    <input type="password" required=""  placeholder="escribe tu clave"><br><br>
    <label>Mensaje:</label>
    <textarea  required="" placeholder="escribe tu mensaje"></textarea><br><br>
    <input type="submit" value="Enviar mensaje">
    </form>

    
asked by Mateo Gomez 19.10.2018 в 00:06
source

3 answers

3

Apart from what colleagues say, there are more typing and logic errors in your code. I indicate them in order of appearance:

  • The variables received in the POST must be controlled: I propose the use of tenarios operators for this.
  • Input a success message in the code before executing: I propose a real evaluation of the inserted rows using rowCount , because it is the only way to know if the query inserted rows.
  • You misspelled the name of the exception: it is PDOException
  • You had poorly written the method that gets the error message: it is getMessage

The code has more improvements: a) It adequately and truly reports what happens; b) Use the variables created in the ternary evaluation for the insertion; c) Create the PDO instance when you are sure that there is valid data to insert ... in short, I call this programming controlled (do not write silent code in which there are eventualities not covered) and logical programming you do not create objects before knowing if you really need to use them.

This would be the corrected code. I hope it is useful.

<?php
    $nombre= (empty($_POST['nombre']))  ? NULL : $_POST['nombre'];
    $clave=  (empty($_POST['clave']))   ? NULL : $_POST['clave'];
    $mensaje=(empty($_POST['mensaje'])) ? NULL : $_POST['mensaje'];

    if ($nombre && $clave && $mensaje) {
        $PDO = new PDO('mysql:host=localhost;dbname=db_mensajes;charset=UTF8','root','' );
        try { 
                $sql=$PDO->prepare("INSERT INTO usuarios( nombre, clave, mensaje) VALUES (:nombre, :clave, :mensaje)");
                $sql->bindParam(':nombre',$nombre);
                $sql->bindParam(':clave',$clave);
                $sql->bindParam(':mensaje',$mensaje);
                $sql->execute();
                $msg="Se insertaron: ".$sql->rowCount()." filas";

         }catch(PDOException $e) {
                echo "mensaje no enviado:".$e ->getMessage();
         }

    }else{

        $msg="No hay datos en las variables del POST";
    }
    echo $msg;   
?> 
    
answered by 19.10.2018 в 01:00
2

You need to add a name to the input so that the form sends it to your php and receives it:

<form method="POST" action="insertar.php">
    <h1>Envio de mensaje</h1>
    <label>Nombre:</label>
    <input type="text" name="nombre" required="" placeholder="escribe tu nombre"><br><br>
    <label>Clave:</label>
    <input type="password" name="clave" required=""  placeholder="escribe tu clave"><br><br>
    <label>Mensaje:</label>
    <textarea  required="" name="mensaje" placeholder="escribe tu mensaje"></textarea><br><br>
    <input type="submit" value="Enviar mensaje">
    </form>
    
answered by 19.10.2018 в 00:12
1

You should have the attribute name in each of your inputs so that at the moment you send them by $_POST interpret them

That is, if in your HTML you do

<input type="text" name="nombre" /> <!--y yo pongo alfredo--> 

On the backend when you do

$nombre = $_POST["nombre"];
  

The value received will be alfredo

<form method="POST" action="insertar.php">
    <h1>Envio de mensaje</h1>
    <label>Nombre:</label>
    <input type="text" name="nombre" required="" placeholder="escribe tu nombre"><br><br>
    <label>Clave:</label>
    <input type="password" name="clave" required=""  placeholder="escribe tu clave"><br><br>
    <label>Mensaje:</label>
    <textarea name="mensaje" required="" placeholder="escribe tu mensaje"></textarea><br><br>
    <input type="submit" value="Enviar mensaje">
    </form>
    
answered by 19.10.2018 в 00:13