0

I have the following query, but my problem is the following:

 $insert_email = "select * from customers";
$run_email = mysqli_query($con, $insert_email);

$find_email = mysqli_fetch_array($run_email);
$demail = $find_email['customer_email'];

echo $demail;

My problem is that at its current state it only returns the first item in this customers table, I would want it to return all of the customer_email in that row and not just the first one.

This is because I would later want to compare it

if($email!= $demail){

where if the email entered by the user during registration is found in the database than I will tell the user the email is already in use by throwing in an else statement.

Update:

$insert_email = "select * from customers where customer_email='$email'";
$run_email = mysqli_query($con, $insert_email);
if(mysqli_num_rows($run_email)>0){

    $crs_id = $_GET['crs_id'];
    $_SESSION['userCoupon'] = $_POST['couponCodeRegisterAmount'];
    $_SESSION['userCouponName'] = $_POST['couponCodeRegister'];


        $_SESSION['customer_email']=$email; 

         $insert_c = "insert into customers (customer_fname,customer_lname,customer_email,customer_pass,coupon_code_register,coupon_code_register_amount) values ('$fname','$lname','$email','$pass','$couponCodeRegister','$couponCodeRegisterAmount')";

        $run_c = mysqli_query($con, $insert_c); 


        echo "<script>window.open('coursePage.php?crs_id=$crs_id#attend','_self')</script>";


        echo "<script>

         document.getElementById('registerError').innerHTML = 'Account has been created successfully, Thanks!'
         </script>";

}

else {
    echo "<script>window.open('coursePage.php?crs_id=$crs_id#attend','_self')</script>";

    echo "<script>
        document.getElementById('registerError').innerHTML = 'This email has already been taken. Please use another one.'
       </script>";
}
Jonathan Etienne
  • 621
  • 3
  • 6
  • 18

3 Answers3

1

You could try using subquery for this, please do excuse for my PHP coding.

SQL: (example - http://sqlfiddle.com/#!9/19101/1)

INSERT INTO customers (email)
SELECT a.email
FROM (SELECT 'not exists email address' AS email) AS a
WHERE NOT EXISTS (SELECT 1 
                  FROM customers cus
                  WHERE a.email = cus.email);

PHP: (you can also use $affected_rows to check if row are inserted)

$todo_email = "not exists email address";

// This is where you will define your insert clause.
$insertClause = "INSERT INTO customers (email)";

// Here you can form a row of record for insert.
// Note: for multiple record use a while loop and use "UNION"
$tmpTableClause = "SELECT %s AS email", 
$tmpTableClause = sprintf($tmpTableClause, mysql_real_escape_string($todo_email));

// This will form the main query logic
$query = "SELECT a.email
          FROM ("+ $tmpTableClause +") AS a
          WHERE NOT EXISTS (SELECT 1 
                            FROM customers cus
                            WHERE a.email = cus.email);";

// Now join and run the real query
$query = $insertClause + $query;
Franky
  • 91
  • 4
0
while ($find_email = mysqli_fetch_array($run_email))
{
  if($email == $find_email['customer_email']){  }
}

or better use this:

$insert_email = "select * from customers where customer_email='".$email."'";
$run_email = mysqli_query($con, $insert_email);
if (mysqli_num_rows($run_email)>0){}
else {echo 'no customers found with this email!'; }
yuk
  • 945
  • 7
  • 14
  • thanks the one concern I have is that with this it does echo out all of email in that row, but then it compares everything that has been echo with the email and hence no accurate comparaison is made if($email!= $demail){ – Jonathan Etienne May 31 '15 at 11:06
  • You need double '==' for comparison – yuk May 31 '15 at 11:16
  • thank you much for your update. It make sense and the code runs, but my problem is that it always hit the else statement regardless if the email exist or not the database which void the whole point of the if statement the query is properly setup – Jonathan Etienne May 31 '15 at 11:41
  • sry, should have been "where customer_email=" – yuk May 31 '15 at 11:43
-1
$insert_email = "select * from customers where email='email_entered'";
$run_email = mysqli_query($con, $insert_email);

if(mysqli_num_rows($run_email)==0){
    //do whatever you wants
}else{
    echo "Email Already Exists";
}
Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
Nikhil
  • 7
  • 2