Mysqli saves the empty records from Php

0

Good morning everyone ..... I have a problem in which my form should send the records to my database, but what it does is keep my records completely empty, I leave my code and hope someone can help me, thanks. .

This is my form, here I assign the names that will be sent to my next page

 <?php
include('conexion.php')
?>

 <body>

<form action="guarda_usuario.php" method="post" enctype="text/plain">
<h1>CONTACTO</h1>

<input type="text" placeholder="Escribe tu nombre" name="nombre" id=""  <br>    
<input type="text" placeholder="Escribe tu apellido" name="apellido" id=""> <br>
<input type="email" placeholder="Escribe tu email" name="email" id="" > <br>
<textarea placeholder="Escriba su comentario" name="comentario"></textarea> <br>
    <select style="width:200px" name="juego" > 
             <option value="0">Selección:</option>
             <?php

             $query = $mysqli -> query ("SELECT * FROM productos");

             while ($valores = mysqli_fetch_array($query)) {

             echo '<option value="'.$valores[id].'">'.$valores[producto].'</option>'; } ?>
   </select> <br> <br>

<input type="submit" value="ENVIAR" name="" id="boton">

</form>
</body>

This is my page where I sent the records to my database, I have that if the variables are empty they are equal to "null" which I think is doing and I do not know why ...

 <?php 


include('conexion.php');
  ?>
<?php
if (isset($_POST['nombre'])) {
$nombre = $_POST['nombre'];
} else {
$nombre = "";
}


  if (isset($_POST['apellido'])) {
  $apellido = $_POST['apellido'];
  } else {
  $apellido = "";
  }

 if (isset($_POST['email'])) {
  $email = $_POST['email'];
  } else {
  $email = "";
  }

   if (isset($_POST['producto'])) {
    $producto = $_POST['producto'];
    } else {
    $producto = "";
 }

    if (isset($_POST['comentario'])) {
    $comentario = $_POST['comentario'];
   } else {
    $comentario = "";
 }

$query="INSERT INTO usuarios (nombre, apellido, email, producto, comentaro) VALUES ('$nombre','$apellido','$email','$producto', '$comentario' )";

$resultado=$mysqli->query($query);

 ?>
    
asked by Fernando Espinosa 28.02.2017 в 17:03
source

4 answers

1

Eliminate the enctype="text/plain" , it is surely influencing you to capture information from the view.

Read this link

    
answered by 01.03.2017 / 13:27
source
0

You have not closed the input tag called "name":

<input type="text" placeholder="Escribe tu nombre" name="nombre" id=""  <br>  

It should be like this:

<input type="text" placeholder="Escribe tu nombre" name="nombre" id="">  <br>  
    
answered by 28.02.2017 в 17:11
0
 <body> <form action="guarda_usuario.php" method="POST"enctype="text/plain">
<h1>CONTACTO</h1>
<input type="text" placeholder="Escribe tu nombre" name="nombre" id="">  <br>    
<input type="text" placeholder="Escribe tu apellido" name="apellido" id=""> <br>
<input type="email" placeholder="Escribe tu email" name="email" id="" > <br>

<textarea placeholder="Escriba su comentario" name="comentario"></textarea> <br>
  <select style="width:200px" name="juego" > 
    <option value="0">Selección:</option>
       <?php
       include 'conexion.php';

       $query = $mysqli -> query ("SELECT * FROM productos");
       while ($valores = mysqli_fetch_assoc($query)) {
 echo '
<option value="'.$valores["id"].'">'.$valores[producto].'</option>
'; } ?>
  </select> <br> <br>

<input type="submit" value="ENVIAR" name="" id="boton">
</form>
</body>



//codigo php

<?php
include 'conexion.php';

$nombre =isset($_POST['nombre']) ? $_POST['nombre'] : null ;
$apellido =isset($_POST['apellido']) ? $_POST['apellido'] : null ;
$email =isset($_POST['email']) ? $_POST['email'] : null ;
$producto = isset($_POST['producto']) ? $_POST['producto'] : null ;
$comentario = isset($_POST['comentario']) ? $_POST['comentario'] : null ;

$query="INSERT INTO usuarios (nombre, apellido, email, producto, comentaro) VALUES ('".$nombre."','".$apellido."','".$email."','".$producto."', '".$comentario."' )";

$resultado=$mysqli->query($query);
if ($resultado) {
    echo "New record created successfully";
} else {
    echo "Error ";
}
 ?>

You did not close the semicolon of the include in the html, I am not an expert in mysqli but I would have used it in assoc instead of the array, in the end I put you there as I would have done, I would like to review your code. connection, I added it in each call to php not separate, and what I told you to check your connection code is by this line .. $ mysqli-> query ($ query); I would have made the connection variable so mysqli_query ($ conn, $ query); finally try the code I gave you .. check it and I hope it works for you.

    
answered by 28.02.2017 в 18:04
0

You can validate if the variable comes with a really empty value through the functions is_null () or empty () , they are used in the same way you are using the isset ().

I attached a link with information regarding the subject.

Is_Null (), Empty (), Isset ()

Greetings, I hope it serves you.

    
answered by 01.03.2017 в 14:09