0

Im trying to make so it checks if the email already exists in the database before inserting the data in it.

Here is my code:

<?php
    $servername = "servername";
    $username = "username";
    $password = "password";
    $dbname = "dbname";

    $Mail = $_POST['email'];

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);

    // Check connection
    if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
    }

    //Inputed in E-Mail Field
    $Mail = $_POST['email'];

    $SearchEmail = $conn->query("SELECT (Mail) FROM betakey WHERE Mail = '$Mail'");

    if ($SearchEmail->num_rows > 0) {
        print "That Email is already registered for the closed alpha";
    }
    else {
        $conn->query("INSERT INTO betakey VALUES ('$Mail')");
    }

    $conn->close();
    ?> 

It doesn't give me any error when i access the page but it also doesn't echo that it exists or inserts the data when it doesn't.

KillZoneZ
  • 11
  • 4

1 Answers1

0

The problem is in your insert query. An insert query is always structured like shown below.

INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...) See: http://www.w3schools.com/php/php_mysql_insert.asp

Applying this to your code, results in the following.

<?php
$servername = "servername";
$username = "username";
$password = "password";
$dbname = "dbname";

$Mail = $_POST['email'];

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

//Inputed in E-Mail Field
$Mail = $_POST['email'];

$SearchEmail = $conn->query("SELECT count(Mail) FROM betakey WHERE Mail = '$Mail'");

if ($SearchEmail->num_rows > 0) {
    print "That Email is already registered for the closed alpha";
}
else {
    $conn->query("INSERT INTO betakey (Mail) VALUES ('$Mail')");
}

$conn->close();
?> 

And I suggest you take away one of the two '$Mail = $_POST['email'];', because it is useless to declare a variable twice in the same way.

P.Yntema
  • 576
  • 2
  • 9
  • 29
  • I don't think thats the answer for it since i tried to register an email that already existed and it didn't give me the "print "(...)" statement. – KillZoneZ Aug 27 '15 at 19:37
  • What is the output of $SearchEmail when you try to print it? – ahmet Aug 27 '15 at 19:45
  • ahmet, this happens Catchable fatal error: Object of class mysqli_result could not be converted to string in /home/a9508844/public_html/BetaRegistration.php on line 31 Thats probably why it isnt working but how do i fix it – KillZoneZ Aug 27 '15 at 19:52
  • Sorry, I'm not good at php but maybe you can try to print as in the answer of @mailo ( http://stackoverflow.com/questions/5157905/mysql-query-result-in-php-variable ) – ahmet Aug 27 '15 at 20:09
  • @KillZoneZ, no, the fatal error happens because you try to print an array. If you want to see the output you should use the print_r($array) funtion. Could you provide us with the full code? At my side it is working when I try to register an email that already exists, so it should work at your side too. – P.Yntema Aug 27 '15 at 21:28