I can not connect my html form to the database

0

I leave my code of the form and the connection ..

    <? php

$conectar=@mysqli_connect('localhost','root','');
if (!$conectar) {
    echo "No se conecto a la base de datos";

}else{
    $base=mysqli_select_db('optica');
    if (!$base) {
        echo "no se encontro la base de datos";
    }
}
$nombre=$_POST['nombre'];
$correo=$_POST['correo'];
$telefono=$_POST['telefono'];
$mensaje=$_POST['mensaje'];

$sql ="INSERT INTO datos VALUES ('$nombre',
'$email',
'$telefono',
'$mensaje')";


$ejecutar=mysql_query($sql);
if (!$ejecutar) {
    echo "Hay algun error";
}else{
    echo "Datos almacenados correctamente";
}
?> 








    </for'<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Contacto</title>
    <link rel="stylesheet" type="text/css" href="css/estiloform.css">
</head>
<body>
    <?php  include_once 'header.php'; ?>
    <form action="enviar.php" method="post">
        <h2>CONTACTANOS</h2>
        <input type="text" name="Nombre" placeholder="Ingresa tu nombre" pattern="áéíóúabcdefghijklmnñopqrstuvwxyz" required="" > 
        <input type="text" name="Correo" placeholder="Ingresa tu correo"pattern="[a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*@[a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{1,5}" required=""> 
        <input type="text" name="Telefono" placeholder="Ingresa tu telefono" pattern="0123456789" required="" > 
       <textarea name="mensaje" placeholder="Escriba su mensaje" required=""></textarea>
       <input type="submit" value="ENVIAR" id="boton">

    </form>
</body>'m>
</body>

    
asked by Andrea5 22.08.2018 в 00:57
source

2 answers

0

You have three errors: The first is that you do not pass the name of the database in the connection, therefore you do not have to connect. The second is that you mix the functions mysql and mysqli . The third the concatenation in variable $sql .

Try this:

$conectar = mysqli_connect('localhost','root','', 'NOMBRE DE LA BASE');

if(!$conectar){
    echo "No se conecto a la base de datos";
}

$sql = "INSERT INTO datos VALUES ('".$_POST['nombre']."', '".$_POST['correo']."', '".$_POST['telefono']."', '".$_POST['mensaje']."')";

$ejecutar = mysqli_query($sql);

if(!$ejecutar){
    echo "Hay algun error";
}else{
    echo "Datos almacenados correctamente";
}

Update : I add a fourth error which is that in the parameter name of your inputs you use uppercase. PHP is a language that distinguishes between uppercase and lowercase.

Update : I take the commented and I tell you that you really should do a verification and validation of the data sent by the form before passing them to the base.

    
answered by 22.08.2018 в 01:51
0

In the sql query, you do not specify which fields in the table the data will go to. And at mysqli_query do not spend the $ connect.

    $conectar = mysqli_connect('localhost','root','', 'NOMBRE DE LA BASE');

    if(!$conectar){
        echo "No conectado";
    }

    $nombre = $_POST['nombre'];
    $correo = $_POST['correo'];
    $telefono = $_POST['telefono'];
    $mensaje = $_POST['mensaje'];

    $sql = "INSERT INTO datos(nombre, correo, telefono, mensaje) VALUES ('$nombre', '$correo', '$telefono', '$mensaje')";

    $ejecutar = mysqli_query($conectar, $sql);

    if(!$ejecutar){
        echo "Hay algun error";
    }else{
        echo "Datos almacenados correctamente";
    }
    
answered by 28.08.2018 в 20:37