Php insert undefined

0

Hi, I searched the forums but I could not find the solution. I'm trying to enter records.

<div align="center">
    <form action="cliente_ingresado.php" method="post">
    <div>
        <label for="identificacion" >Identificación:  </label> &nbsp;
        <input type="text" name="identificacion" id="identificacion" required="required"/>
    </div>

    <div>
        <label for="nombre" >Nombre: </label>&nbsp;
        <input type="text" name="nombre" id="nombre" required="required"/>
    </div>

    <div>
        <label for="direccion">Dirección: </label>&nbsp;
        <input type="text" name="direccion" id="direccion" />
    </div>

    <div>
        <label for="telefono">Teléfono: </label>&nbsp;
        <input type="text" id="telefono" name="telefono"/>
    </div>

    <div>
        <label for="correo">Correo: </label>&nbsp;
        <input type="email" id="correo" name="correo"/>
    </div>

    <div>
        <label for="sexo">Sexo: </label>&nbsp;
        <input type="text" id="sexo" name="sexo"/>
    </div>

    <div>
        <label for="estado_civil">Estado Civil:  </label>&nbsp;
        <input type="text" id="estado_civil" name="estado_civil"/>
    </div>

    <div class="button">
        <button type="submit" name="submit">Guardar</button> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;


        <button type="button">Limpiar</button>
    </div>
</form>

Here process:

echo $_POST['identificación'];
    if(isset($_POST['submit']) && !empty($_POST['submit'])) {
        $identificacion = $_POST['identificación'];
        $nombre = $_POST['nombre'];
        $direccion = $_POST['direccion'];
        $telefono = $_POST['telefono'];
        $correo = $_POST['correo'];
        $sexo = $_POST['sexo'];
        $estadoCivil = $_POST['estadoCivil'];

        $insertar = "INSERT INTO tbl_clientes (identificacion, nombre, direccion, telefono, correo, sexo, estado_civil) VALUES ('$identificacion', '$nombre', '$direccion', '$telefono', '$correo', '$sexo', '$estado_civil')";

        $ejecutar = mysqli_query($con, $insertar);

        echo $ejecutar;
        if($ejecutar)
        {
            echo "Insertado correctamente";
        }
        else 
            {
                echo "Error";
            }

        mysqli_close($con);

    }
    else
    {
        echo "Insert no definido";
    }

But he tells me he is undefined

    
asked by Jhon Hernández 31.05.2018 в 20:38
source

3 answers

0

Verify:

  • The variables $ _POST [""] you receive are the same as those declared on your form.
  • Avoid the use of accents in variables.

isset ()

  

Determines if a variable is defined and is not NULL

empty ()

  

Determine if a variable is considered empty. A variable is   considered empty if it does not exist or if its value is equal to FALSE. empty ()   it does not generate a warning if the variable does not exist.

if(isset($_POST['submit'])) {

    if (!empty($_POST['identificacion']) && !empty($_POST['nombre']) && !empty($_POST['direccion']) &&
        !empty($_POST['telefono']) && !empty($_POST['correo']) && !empty($_POST['sexo']) && !empty($_POST['estado_civil'])) {

        $identificacion = $_POST['identificacion'];
        $nombre = $_POST['nombre'];
        $direccion = $_POST['direccion'];
        $telefono = $_POST['telefono'];
        $correo = $_POST['correo'];
        $sexo = $_POST['sexo'];
        $estadoCivil = $_POST['estado_civil'];

        $insertar = "INSERT INTO tbl_clientes (identificacion, nombre, direccion, telefono, correo, sexo, estado_civil) VALUES ('$identificacion', '$nombre', '$direccion', '$telefono', '$correo', '$sexo', '$estado_civil')";

        $ejecutar = mysqli_query($con, $insertar);

        if($ejecutar){
            echo "Insertado correctamente";
        }else {
                echo "Error";
            }
        } else {
            echo "Datos vacios.";
        }
        mysqli_close($con);
    }else{
        echo "Insert no definido";
    }

Recommendations:

  

Using isset () and empty () -    Publication

     

How to avoid SQL injection -    Publication

    
answered by 01.06.2018 / 00:08
source
0

I think the error is in $ _POST ['submit'], there is not a variable there simply is the send button, so when the variable does not arrive it will always be marked as false.

    
answered by 31.05.2018 в 21:18
0

I think you should ask if your form boxes are set instead of 'isset ($ _ POST [' submit '])'

if(isset($_POST['direccion']) && isset($_POST['nombre']) && isset($_POST['direccion']))
{
    //tu codigo...
}

I also think you should change in your php code:

$_POST['estadoCivil'] por $_POST['estado_civil']

since in your form the name that has the input is civil status and not Civil status

    
answered by 31.05.2018 в 23:26