Does not record form data in the BD

0

Good, yesterday I spent hours and hours trying code to register the data of a form in my database on an external server and I did not get it, I was asking and they passed me a code that worked for them but to me no, I'll show it to you next in case you see where the error is.

Code that supposedly inserts the data

<?php

$conexion =new mysqli("mi_host","mi_usuario","mi_contraseña","nombre_de_base_de_datos");

if(mysqli_connect_errno())
{
		echo 'Conexion Fallida : ', mysqli_connect_error();
		exit();
}


$numero = $_POST['numero'];
$fecha = $_POST['fecha'];
$cliente = $_POST['cliente'];
$direccion = $_POST['direccion'];
$telefono = $_POST['telefono'];
$averia = $_POST['averia'];
$estado = $_POST['estado'];
$observaciones = $_POST['observaciones'];


$sql = "INSERT INTO tabla (numero,fecha,cliente,direccion,telefono,averia,estado,observaciones) VALUES ('$numero','$fecha','$cliente','$direccion','$telefono','$averia','$estado','$observaciones')";


mysqli_query($sql,$conexion);


if ($sql>0)
{
echo "<script type='text/javascript'>
alert ('Los datos se guardaron exitosamente');
window.location='phpparameters-02.php';
</script>";
}
else
{
echo "<script type='text/javascript'>
alert ('Error al guardar');
window.location='crear.php';
</script>";
}
?>

Form page

<html>

<head>
<title>Guardar datos en una base de datos</title>
</head>

<body>

<form method="post" action="insertar.php" name="form">

    <p>Numero de Aviso <input type="number" name="numero" size="20"></p>
    <p>Fecha <input type="date" name="fecha"></p>
    <p>Cliente <input type="text" name="cliente"></p>
    <p>Direccion <input type="text" name="direccion"></p>
    <p>Telefono <input type="number" name="telefono"></p>
    <p>Averia <input type="text" name="averia"></p>
    <p>Estado <input type="text" name="estado"></p>
    <p>Observaciones <input type="text" name="observaciones" size="20"></p>

    <p><input type="submit" value="Guardar datos" name="b1"></p>
</form>


</body>

</html>

    
asked by Alejandro 28.12.2017 в 10:22
source

1 answer

2

I think you should only change the last part of the PHP .

$result = mysqli_query($sql,$conexion);


if ($result >0)
{
echo "<script type='text/javascript'>
alert ('Los datos se guardaron exitosamente');
window.location='phpparameters-02.php';
</script>";
}
else
{
echo "<script type='text/javascript'>
alert ('Error al guardar');
window.location='crear.php';
</script>";
}

After launching the query, you try to check the number of records that have been updated, but at no time do you keep that data in your variable $sql .

To avoid problems with the names and know what is being referenced, I have called it $result .

Edited Try putting the code in this way

try {
    $conexion =new mysqli("mi_host","mi_usuario","mi_contraseña","nombre_de_base_de_datos");

    if(mysqli_connect_errno())
    {
            echo 'Conexion Fallida : ', mysqli_connect_error();
            exit();
    }


    $numero = $_POST['numero'];
    $fecha = $_POST['fecha'];
    $cliente = $_POST['cliente'];
    $direccion = $_POST['direccion'];
    $telefono = $_POST['telefono'];
    $averia = $_POST['averia'];
    $estado = $_POST['estado'];
    $observaciones = $_POST['observaciones'];


    $sql = "INSERT INTO tabla (numero,fecha,cliente,direccion,telefono,averia,estado,observaciones) VALUES ('$numero','$fecha','$cliente','$direccion','$telefono','$averia','$estado','$observaciones')";

   $result = mysqli_query($sql,$conexion);


    if ($result >0)
    {
        echo "<script type='text/javascript'>
            alert ('Los datos se guardaron exitosamente');
            window.location='phpparameters-02.php';
        </script>";
    }
    else
    {
        echo "<script type='text/javascript'>
            alert ('Error al guardar');
            window.location='crear.php';
        </script>";
    }

} catch (Exception $e) {
    echo 'Excepción capturada: ',  $e->getMessage(), "\n";
}

Solution Change the insert and put it this way

$sql = "INSERT INTO tabla (numero,fecha,cliente,direccion,telefono,averia,estado,observaciones) 
            VALUES ('$numero','$fecha','$cliente','$direccion','$telefono','$averia','$estado','$observaciones')";

            if ($conexion->query($sql) === TRUE) {
                echo "registro insertado";
            } else {
                echo "Error: " . $sql . "<br>" . $conexion->error;
            }

            $conexion->close();
    
answered by 28.12.2017 / 11:35
source