Form does not save data in database

0

I have a simple system to store data in a database through the following form:

Conexion.php

<?php
$host="localhost";
$usua="root";
$pass="";
$base="yerson"; //base de datos//
$cone = new mysqli($host,$usua,$pass,$base);

if ($cone->connect_error) 
{
    die('No se pudo conectar a la base de datos:'.$cone->connect_error);
}

?>

Form.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>formulario</title>
<style type="text/css">
body {
    background-image: url(1.png);
}
.yer {
    font-family: Arial Black, Gadget, sans-serif;
}
.yer {
    font-family: Arial Black, Gadget, sans-serif;
}
.ter {
    font-family: Verdana, Geneva, sans-serif;
}
.yer {
    font-family: Arial Black, Gadget, sans-serif;
}
.yer {
    font-family: Arial Black, Gadget, sans-serif;
}
.yer {
    font-family: Arial Black, Gadget, sans-serif;
}
</style>
</head>

<body>
<center>
  <h1><font color="#00FF00" class="yer">Bienvenido a Nuestro Sistema de Registro 
</font></h1></center>
<marquee>
<h2><font color="#FFFF00"> <span class="yer">REGISTRATE.. YA</span>.</font></h2></marquee>
<form action="procesar.php"method="post" name="form">
<table width"200" border="0">
<tr>
<font color="#00FF00">
<td class="yer">nombres</td></font>
<td><input type="text" name="nombres" /></td>
</tr>
<tr>
<font color="#FFFF00"><td class="yer">apellidos</td></font>
<td><input type="text" name="apellidos" /></td>
</tr>
<tr>
<font color="#00FF00"><td class="yer">telefono</td></font>
<td><input type="text" name="telefono" /></td>
</tr>
<tr>
<font color="#FFFF00"><td class="yer">correo_electronico</td></font>
<td><input type="text" name="correo_electronico" /></td>
</tr>
<tr>
<font color="#00FF00"><td class="yer">ciudad</td></font>
<td><input type="text" name="ciudad" /></td>
</tr>
<tr>
<td>&nbsp;</td>
<center><td><input type="submit" value="insertar dactos" /> <input type="reset" value="limpiar" /> </td></center>
</tr>

</table>
</form>
</body>
</html>

Process.php

    <?php
        include ("conexion.php"); 
        if (isset($_POST ['nombres'] ) && !empty ($db_POST ['nombres']) &&
        isset($_POST ['apellidos'] ) && !empty ($db_POST ['apellidos'] ) &&
        isset($_POST ['telefono'] ) && !empty ($db_POST ['telefono'] ) &&
        isset($_POST ['correo_electronico'] ) && !empty ($db_POST ['correo_electronico'] ) &&
        isset($_POST ['ciudad'] ) && !empty ($db_POST ['ciudad'] ))
        {

        $nomb=$_REQUEST['nombres'];
        $apel=$_REQUEST['apellidos'];
        $tele=$_REQUEST['telefonos'];
        $corr=$_REQUEST['correo_electronico'];
        $ciud=$_REQUEST['ciudad'];
        mysqli_query("INSERT INTO datos (nombres,apellidos,correo_electronico,telefonos,ciudad) 
        VALUES ('$nomb','$apel','$tele','$corr','$ciud') ") or die (mysql_error()); 
echo "datos insertados correctamente";
}else{
    echo "problemas al insertar los datos";
}

        ?>

The problem: after processing the information the page gives the data error and does not save anything in the database

    
asked by Victor Alejandro Alvarado Vilo 10.11.2016 в 01:40
source

2 answers

1

As you mentioned, you are mixing your code a bit.

The first error I see in proces.php is that you do not pass the value correctly by the function empty() .

//Tu código.
!empty ($db_POST['nombres'])
        ^^^^^^^^

//Actualizado
!empty($_POST['nombres'])

Second error, your sentence will never insert the data, since you forgot to add the parameter of your connection to the function mysqli_query () , and in die() you use one mysql *

//Tu código
mysqli_query("INSERT INTO datos nombres,apellidos,correo_electronico,telefonos,ciudad) VALUES
           ^^^^
'$nomb','$apel','$tele','$corr','$ciud') ") or die (mysql_error());
                                                      ^^^^^^ ^^^^

Another thing to keep in mind is that you create your connection correctly by style by procedures , which is what you are using.

You could improve and optimize your code in the following way:

conexion.php (Style by procedures).

<?php
$cone = @mysqli_connect('localhost', 'root', '', 'yerson');

if (!$cone) {
    exit('Error de conexión: ' . mysqli_connect_error());
}
?>

HTML form

<form method="POST" action="procesar.php">

   <input type="text" name="nombres" placeholder="Nombre" />
   <input type="text" name="apellidos" placeholder="Apellidos" />
   <input type="text" name="telefono"  placeholder="Telefono" />
   <input type="text" name="correo_electronico" placeholder="Correo electronico" />
   <input type="text" name="ciudad" placeholder="Cuidad" />

   <input type="submit" name="insertar_datos" value="insertar dactos" /> 
   <input type="reset" value="limpiar" /> 

</form>

proces.php

<?php
//Reseteamos variables.
$mensaje = $nombres = $apellidos = $telefono = $correo_electronico = $ciudad = NULL;

//Comprobamos si nuestro formulario está definido y no es NULL (mediante el identificador de nuestro botón de envió).
if (isset($_POST['insertar_datos'])) {

    //Comprobamos que nuestros inputs no estén vacíos.
    if (empty($_POST['nombres']) && empty($_POST['apellidos']) && empty($_POST['telefono']) && empty($_POST['correo_electronico']) && empty($_POST['ciudad'])) {
        $mensaje = 'Por favor todos los campos son obligatorios.'
    } else {
        //Requeremos conexión.
        require_once'conexion.php';

        //Obtenemos datos, para mayor seguridad utiliza la función mysqli_real_escape_string.
        $nombres = mysqli_real_escape_string($cone,$_POST['nombres']);
        $apellidos = mysqli_real_escape_string($cone,$_POST['apellidos']);
        $telefono = mysqli_real_escape_string($cone,$_POST['telefono']);
        $correo_electronico = mysqli_real_escape_string($cone,$_POST['correo_electronico']);
        $ciudad = mysqli_real_escape_string($cone,$_POST['cuidad']);

        //Si nuestras variables estan definidas, continuamos...
        if ($nombres && $apellidos && $telefono && $correo_electronico && $ciudad) {
            //Insertamos nuestros datos a la Base de datos.
            $sentencia = mysqli_query($cone, "INSERT INTO datos (nombres,apellidos,correo_electronico,telefonos,ciudad) VALUES ('$nombres','$apellidos','$telefono','$correo_electronico','$ciudad')"); 

            //Comprobamos que se ejecuto correctamente.
            if(!$sentencia) { #falso.
               printf("Error en ejecutar sentencia: %s\n", mysqli_error($cone));
            } else { #verdadero.
                //Cierra la conexión
                mysqli_close($cone);
                $mensaje = 'Los datos se insertaron correctamente.'
            }

        }
    }
} else { unset($_POST); }

//Alertas.
echo $mensaje;

?>

Even so, I advise you to use sentences prepared or PDO , for greater security against attacks from SQL injection .

Greetings!

    
answered by 18.03.2017 в 22:43
0

What happens is that you are using the MySQLi object-oriented interface, that is, you are using $cone = new mysqli($host,$usua,$pass,$base); and to save you are using the other procedural MySQLi interface mysqli_query("INSERT INTO datos ...)

To save the data you must use the object-oriented form, that is, you must change mysqli_query() by:

$sql = "INSERT INTO datos (nombres,apellidos,correo_electronico,telefonos,ciudad) 
    VALUES ('{$nomb}','{$apel}','{$tele}','{$corr}','{$ciud}')";
$insertar = $cone->query($sql);
if($insertar === TRUE){
    echo "Datos insertados";
}else{
    echo $cone->error;
}
$cone->close();

You also have another error in the validation of the data, since you are using !empty($db_POST['nombres'] . You have to correct it, it should be !empty($_POST['nombres']) . Since the variable $db_POST does not exist

    
answered by 10.11.2016 в 17:15