0

In my PHP file I want to check if the email is already taken before inserting the data into my mysql database.

I cant find anything wrong with my code but it wont work.

PHP code:

<?php


$conn = mysqli_connect("s.amazonaws.com", "username", "pass", "SellerAccounts");

//If statement echos back to the browser if we connected to the server or not.

    //Store the data from the POST (text from the user) into a variable.
    $Sellers_CompanyName = $_POST["SellersCompanyName"];
    $Sellers_CompanyWebsite = $_POST["SellersCompanyWebsite"];
    $Sellers_IndustryName = $_POST["SellersIndustryName"];
    $Sellers_SecondaryIndustryName = $_POST["SellersSecondaryIndustryName"];
    $Sellers_FirstContactFirstName = $_POST["SellersFirstContactFirstName"];
    $Sellers_FirstContactLastName = $_POST["SellersFirstContactLastName"];
    $Sellers_FirstContactNumber = $_POST["SellersFirstContactNumber"];
    $Sellers_FirstContactEmail = $_POST["SellersFirstContactEmail"];
    $Sellers_SecondContactFirstName =    $_POST["SellersSecondContactFirstName"];
    $Sellers_SecondContactLastName = $_POST["SellersSecondContactLastName"];
    $Sellers_SecondContactNumber = $_POST["SellersSecondContactNumber"];
    $Sellers_SecondContactEmail = $_POST["SellersSecondContactEmail"];
    $Sellers_Password = $_POST["Password"];


    $result = mysql_query("SELECT * FROM user_info WHERE     Sellers_FirstContactEmail = '".$Sellers_FirstContactEmail."'");

            if ( mysql_num_rows($result) > 0 ){

     echo("Email is already in use");

            }else{
 //
    $statement = mysqli_prepare($conn, "INSERT INTO user_info                (Sellers_CompanyName, Sellers_CompanyWebsite, Sellers_IndustryName,     Sellers_SecondaryIndustryName, Sellers_FirstContactFirstName, Sellers_FirstContactLastName, Sellers_FirstContactNumber, Sellers_FirstContactEmail, Sellers_SecondContactFirstName, Sellers_SecondContactLastName, Sellers_SecondContactNumber, Sellers_SecondContactEmail, Sellers_Password) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ");

mysqli_stmt_bind_param($statement, "sssssssssssss", $Sellers_CompanyName,    $Sellers_CompanyWebsite, $Sellers_IndustryName, $Sellers_SecondaryIndustryName, $Sellers_FirstContactFirstName, $Sellers_FirstContactLastName, $Sellers_FirstContactNumber, $Sellers_FirstContactEmail, $Sellers_SecondContactFirstName, $Sellers_SecondContactLastName, $Sellers_SecondContactNumber, $Sellers_SecondContactEmail, $Sellers_Password);
    mysqli_stmt_execute($statement);
    mysqli_stmt_close($statement);

    if(mysqli_query($connection, $sql_query)){
       // echo" Data insertion success...";
     }
            }

mysqli_close($conn);

?>

i can insert everything fine and connect to the DB

Lazar
  • 99
  • 8
  • What's your table structure? Did you select the correct column? There's no problem with your SQL query statement, if you selected the correct column. – Panda Mar 06 '16 at 02:31
  • The error is not in the code above. Maybe in the connection. Or the column name is not `Sellers_FirstContactEmail`. You have to provide more code. – fusion3k Mar 06 '16 at 02:33
  • yea in my table user_info i have a column called Sellers_FirstContactEmail – Lazar Mar 06 '16 at 02:34
  • 1
    Try echoing out `$Sellers_FirstContactEmail` to see if it holds anything – Panda Mar 06 '16 at 02:35
  • i can add more code if you would like – Lazar Mar 06 '16 at 02:35
  • We need more info or more code to help you. Also double-check that `$Sellers_FirstContactEmail` is not empty. Also check if mysql query returns errors... – fusion3k Mar 06 '16 at 02:37
  • here ill update the code – Lazar Mar 06 '16 at 02:38

2 Answers2

1

You cannot mix MySQL functions with MySQLi functions. And also your connection is $conn, so using $connection will not work.

<?php

$conn = mysqli_connect("s.amazonaws.com", "username", "pass", "SellerAccounts");

$Sellers_CompanyName = $_POST["SellersCompanyName"];
$Sellers_CompanyWebsite = $_POST["SellersCompanyWebsite"];
$Sellers_IndustryName = $_POST["SellersIndustryName"];
$Sellers_SecondaryIndustryName = $_POST["SellersSecondaryIndustryName"];
$Sellers_FirstContactFirstName = $_POST["SellersFirstContactFirstName"];
$Sellers_FirstContactLastName = $_POST["SellersFirstContactLastName"];
$Sellers_FirstContactNumber = $_POST["SellersFirstContactNumber"];
$Sellers_FirstContactEmail = $_POST["SellersFirstContactEmail"];
$Sellers_SecondContactFirstName =    $_POST["SellersSecondContactFirstName"];
$Sellers_SecondContactLastName = $_POST["SellersSecondContactLastName"];
$Sellers_SecondContactNumber = $_POST["SellersSecondContactNumber"];
$Sellers_SecondContactEmail = $_POST["SellersSecondContactEmail"];
$Sellers_Password = $_POST["Password"];


$result = mysqli_query($conn, "SELECT * FROM user_info WHERE     Sellers_FirstContactEmail = '$Sellers_FirstContactEmail'");

if ( mysqli_num_rows($result) > 0 ){

    echo("Email is already in use");

} else {

$statement = mysqli_prepare($conn, "INSERT INTO user_info                (Sellers_CompanyName, Sellers_CompanyWebsite, Sellers_IndustryName,     Sellers_SecondaryIndustryName, Sellers_FirstContactFirstName, Sellers_FirstContactLastName, Sellers_FirstContactNumber, Sellers_FirstContactEmail, Sellers_SecondContactFirstName, Sellers_SecondContactLastName, Sellers_SecondContactNumber, Sellers_SecondContactEmail, Sellers_Password) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ");

mysqli_stmt_bind_param($statement, "sssssssssssss", $Sellers_CompanyName,    $Sellers_CompanyWebsite, $Sellers_IndustryName, $Sellers_SecondaryIndustryName, $Sellers_FirstContactFirstName, $Sellers_FirstContactLastName, $Sellers_FirstContactNumber, $Sellers_FirstContactEmail, $Sellers_SecondContactFirstName, $Sellers_SecondContactLastName, $Sellers_SecondContactNumber, $Sellers_SecondContactEmail, $Sellers_Password);

mysqli_stmt_execute($statement);

mysqli_stmt_close($statement);

    if(mysqli_query($conn, $sql_query)){
        // echo" Data insertion success...";
    }
}

mysqli_close($conn);

?>
Panda
  • 6,955
  • 6
  • 40
  • 55
-1

how about you replace your query with the following?:

$result = mysql_query("SELECT * FROM user_info WHERE Sellers_FirstContactEmail = '".$Sellers_FirstContactEmail."'");

Note the use of string concatenation in the above. In your case, your query is trying to find out emails that match the string "$Sellers_FirstContactEmail", which is not intended - you want to check for the value of that variable.

Ashraf Iqbal
  • 412
  • 3
  • 11
  • Actually, your solution is **identical** to the OP original query. The OP query **don't** find out emails that match the string "$Sellers_FirstContactEmail". – fusion3k Mar 06 '16 at 02:31
  • not really identical, see the use of dots (.) in my solution which is absent in the original query. @Lazar, do you have a php variable called $Sellers_FirstContactEmail and that contains the correct email address you are querying for? – Ashraf Iqbal Mar 06 '16 at 02:37
  • 1
    @AshrafIqbal It's the same with the use of dots and without – Panda Mar 06 '16 at 02:39