How to insert a database contents from a form in php

0

I would like to know how to insert content from a PHP form into a MySQL database. Then I show you my code and the error that appears to me.

This is the code of the form:

<?php 
include 'menu.php';

 ?>
 <br>
 <form method="POST" action="ingresar1.php">
CEDULA:<input type="text" name="cedula"><br>
NOMBRE:<input type="text" name="nombre"><br>
APELLIDO:<input type="text" name="apellido"><br>
DIRECCION:<textarea name="direccion" rows="5" cols="25"></textarea><br>
EMAIL:<input type="text" name="email"><br>
TELEFONO:<input type="text" name="telefono"><br>
SEXO:<input type="radio" name="sexo" value="F">FEMENINO<br>
<input type="radio" name="sexo" value="M">MASCULINO<br>

<input type="submit" name="Ingresar" value="Ingresar">
<input type="reset" name="Limpiar" value="Limpiar">

 </form>

This is the PHP code that inserts the data in the DB:

<?php 
include 'conexion.php';
 $sql = "INSERT INTO usuarios  ('$_POST [cedula]',
                                        '$_POST [nombre]',
                                        '$_POST [apellido]',
                                        '$_POST [direccion]',
                                        '$_POST [email]',
                                        '$_POST [telefono]',
                                        '$_POST [sexo]')";
$resultado = mysqli_query($link,$sql);
echo mysqli_error($link);
if (!mysqli_error($link)) {
    ?>
    <script>
        alert ('Registro fue ingresado con exito');
    </script>
    <?php
}
else
{
    ?>
    <script>
        alert ('ERROR: El registro no fue almacenado');
    </script>
    <?php
}
?>
<meta http-equiv="refresh" content="10;url=ingresar.php">

After sending the data I get this:

I'll accept and I get this:

but I've seen the code and I do not know what my error might be.

    
asked by Junior Mendes 10.06.2018 в 23:27
source

3 answers

0

Try this to see:

<?php
$db_host = 'localhost';
$db_nombre = 'database_name';
$db_usuario = 'root';
$db_clave = '';

$conexion = mysqli_connect($db_host, $db_usuario, $db_clave, $db_nombre);

if(mysqli_connect_errno()) {
    echo "fallo";
    exit();
}

$conexion->set_charset("utf8");

$consulta = "INSERT INTO usuarios(cedula, nombre, apellido, direccion, email, telefono, sexo) VALUES ('" . $_POST[cedula] . "','" . $_POST[nombre] . "','" .  $_POST[apellido] . "','" .  $_POST[direccion] . "','" .  $_POST[email] . "','" .  $_POST[telefono] . "','" .  $_POST[sexo] . "')";

$resultado = mysqli_query($conexion, $consulta);
?>
    
answered by 11.06.2018 / 02:45
source
0

You could move the $ _POST to variables and then perform the insert     

$cedula   = $_POST['cedula'];
$nombre   = $_POST['nombre'];
$apellido = $_POST['apellido'] ;
$direccion = $_POST['direccion'] ;
$email     = $_POST['email'] ;
$telefono   = $_POST['telefono'] ;
$sexo      = $_POST['sexo'] ;

 $sql = "INSERT INTO usuarios  values ( '$cedula', '$nombre', '$apellido', '$direccion', '$email', '$telefono', '$sexo' )" ;
 echo $sql ;                 
if (mysqli_query($link, $sql)) {
    echo "Registro insertado correctamente";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

?>
<meta http-equiv="refresh" content="10;url=ingresar.php">
    
answered by 11.06.2018 в 02:53
0

Explanation of the answers given:

The error is quite explicit "Array to string conversion"

When you put a space between $_POST and the index you want to reference, take the array ( $_POST is an array) and try to convert it to string (in the sql it remains as the word "Array").

Then the space and the index are taken literally, that's why the sql is half bizarre: " Array [cedula]', 'Array [nombre]' ", etc ...

If you remove the spaces between $_POST and [ , you will probably see another error / warning: when referencing $_POST[nombre] php you find that nombre is not a constant and the value "nombre" is assumed, for avoid this put $_POST["nombre"] .

Which means: the value of the key nombre in the array $_POST

    
answered by 11.06.2018 в 03:29