msoboc4
msoboc4

Reputation: 93

php check if email in database

Hy I'm trying to validate the email address in a registration form. I want to check if the address exists in the database. this is my first time with php and i have no idea how to. And i tried to add in my code and error msg, if the email has bad characters.

<?php


$dbhost = "localhost";
$dbusername = "test";
$dbpassword = "tester";
$database_name = "dbtest";


$link = mysql_connect("$dbhost", "$dbusername", "$dbpassword");
if(!$link){ die('Could not connect: ' . mysql_error());}

$db_selected = mysql_select_db("$database_name",$link);
if(!$db_selected){ die('Can\'t use foo: ' . mysql_error());}
            

        $email = strip_tags($_POST['email']);
        $verzija = strip_tags($_POST['mydropdown2']);
        $model = strip_tags($_POST['mydropdown']);
        
        $sql = mysql_query("INSERT INTO novice (naslov, podnaslov, vsebina, objavljeno)
                            VALUES ('$email', '$verzija', '$model', now())
                            ");

if(mysqli_query($link, $sql)){
    echo "Records added successfully.";
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}





// close connection

?>
<form action="insert.php" method="post">
        <label for="model">Phone model:</label>
        <select required aria-required="true" id="model" name="mydropdown">
            <option value="">Select...</option>
            <option value="x">x</option>
            <option value="xx">xx</option>
            
        </select>
        <label for="version">IOS version:</label>
        <select required aria-required="true" id="version" name="mydropdown2">
            <option value="">Select...</option>
            <option value="ax">ax</option>
            <option value="bx">bx</option>
            <option value="cx">cx</option>
        </select>
        <p>
            <br>
            <label for="emailAddress">Email Address:</label>
            <input type="text" id="emailAddress" placeholder="Your email..." name="email" required> </p>
        <input type="submit" value="Submit"> </form>

make it.

Upvotes: 0

Views: 120

Answers (1)

user5820249
user5820249

Reputation:

Now you are trying to insert and not validating !

Use (using PDO) :

$db = new PDO ("mysql:host=".$hostname.";dbname=".$dbname, $username, $password);

$query = $db -> prepare ("SELECT * FROM test WHERE email = :email");

$query -> execute (array (":email" => $email));

$count = $query -> rowCount();

if($count > "0")
{
 echo "email already exist";
}

And you can use this :

filter_var($email, FILTER_VALIDATE_EMAIL);

To validate email format.

Upvotes: 5

Related Questions