Check if there is mail in BD [closed]

0

You will see I have a form in which I have a name, last name, two mail fields (the second to verify if it is equal to the first one), a password, and two RadioButton with the sexes.

What I need is to check only the mail if it is repeated or not in the BD. the other fields are valid with JQuery. But I will also use JQuery to check that the two emails are the same (in both fields).

I'm using PHP.

The first question is: Can I connect to the bd in a separate file and use "Include ()" ?? or can I use it in the same file where I used the email (where I sent the form with $ _POST)?.

Could you help me with the code? It's not that I want them to do everything to me, but the basics. Just check the mail if it already exists on the DB.

    
asked by luis 23.08.2016 в 04:04
source

1 answer

1

Of course you can include the connection to the database in a separate file with the function include .

Suppose that our connection file is called connection.php

<?php
   $servername = "localhost";
   $user = "pepito";
   $password = "mipass";
   $dbname = "nombredebd";


   //Creamos la conexion a la base de datos
   $connection = new mysqli($servername, $user, $password, $dbname);

   //Verificamos la conexion a la BD. Si la conexion falla, nos mostrará el error, si no la conexión se estableció.
   if ($connection->connect_error) {
       die("La conexión fallo. Error: (".$connection->connect_errno.") ".$connection->connect_error);
   }
?>

Then we will proceed to 'include' our file connection.php , to the file where we will verify if the mail exists in the database or not.

<?php
    include 'connection.php';
    //Como tu quieres utilizar el método POST, lo haremos de la siguiente manera. 
    //La variable $_SERVER es una variable reservada que incluye cierta información del propio servidor. 
    //Lo que quiere decir que el 'metodo de peticion' (REQUEST_METHOD) se utilizara solo SI la peticion es mediante POST.
    if($_SERVER["REQUEST_METHOD"] == "POST"){
         $correo = $_POST["correo"];

         //Procederemos a hacer una consulta que buscara el correo del usuario
         $buscarCorreo = "SELECT * from usuarios WHERE correo='$correo'";

         //Realizamos la consulta y anadimos $connection, ya que es la variable que creamos en nuestro archivo connection.php
         $resultado = $connection->query($buscarCorreo);

         //Usaremos la funcion mysqli_num_rows en la consulta $resultado,
         //esta funcion nos regresa el numero de filas en el resultado
         $contador = mysqli_num_rows($resultado);

         //SI SI EXISTE una fila, quiere decir QUE SI ESTA EL CORREO EN LA BASE DE DATOS
         if($contador == 1) {
            echo 'El correo ya existe';
         } else {
         echo 'El correo no existe';
         }
    }
?>

I try to explain it as best I can, even so, if you have any questions leave me a comment and not an answer. So I can help or guide you if you have any further questions.

    
answered by 23.08.2016 / 04:40
source