Jump to content

Recommended Posts

I have written this code to register a consultant, but I want it to check if the email address already exists. I have attempted writing the if statement but it doesn't work. If anyone could have a look at it and point out were I am going wrong it would be much appreciated.

<?php
  require "dbconn2.php";
  $first_name = $_POST['first_name'];
  $last_name = $_POST['last_name'];
  $address_first_line = $_POST['address_first_line'];
  $post_code = $_POST['post_code'];
  $email_address = $_POST['email_address'];  
  $phone_number = $_POST['phone_number'];  
  $y = $_POST['y'];
  $m = $_POST['m'];
  $d = $_POST['d'];
  $rate_id = $_POST['rate'];
  $passwd = $_POST['passwd'];
  $enc = sha1($passwd);
  $dob = $y."-".$m."-".$d." ".$_POST["dob"];
  
  $sql = "SELECT * FROM consultant WHERE email_address = '$email_address'";
  if (mysql_num_rows == 0)
  {
  
  $sql = "INSERT INTO consultant VALUES (0,'".$first_name."','".$last_name."','".$address_first_line."','".$post_code."',
									 '".$email_address."','".$phone_number."','".$dob."','".$rate."','".$enc."')";
  $result = mysql_query ($sql, $connection)
    or die ("Couldn't perform query $sql <br />".mysql_error());
  header("Location: addconsultantform.php");
  exit();
  }
  else{
  echo "Sorry but it appears that an account with this email address already exists.";
   header("Location: addconsultantform.php");
  exit();
  }

?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Add Consultant</title>

</head>

<body>

</body>
</html>

Thanks and regs. 8)

Link to comment
https://forums.phpfreaks.com/topic/150877-solved-email-validation/
Share on other sites

Why are you redefining all the variables? Considering variables are passed by value this essentially results in double memory consumption, which is a waste because you aren't doing anything new with them.

 

Anyway, you're never running the query, so change

$sql = "SELECT * FROM consultant WHERE email_address = '$email_address'";
if (mysql_num_rows == 0)

to

$sql = mysql_query("SELECT * FROM consultant WHERE email_address = '$email_address'");
if (mysql_num_rows($sql) == 0)

 

You also need to escape your values or you're vulnerable to SQL injection. See: mysql_real_escape_string

I'm thinking your problem is with your  mysql_num_rows http://ca2.php.net/mysql_num_rows

 

you have ..

 

$sql = "SELECT * FROM consultant WHERE email_address = '$email_address'";
  if (mysql_num_rows == 0)
  {

 

Your not running the query to even get the row, I could be wrong. Try something like this

 

$sql = "SELECT * FROM consultant WHERE email_address = '$email_address'";
$result = mysql_query($sql);
  if (mysql_num_rows($result) == 0)
  {


Thanks Daniel0, I am aware of the sql injection and plan to remedy this once I have the form validation sorted. I don't know why I redefine my variables it was just the way I have been taught at uni, I am putting this together for my dissertation so I know I still have a lot to learn.

 

  echo "Sorry but it appears that an account with this email address already exists.";
   header("Location: addconsultantform.php");
  exit();

This just echos the message and fails to arrive at the header?

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.