检查数据库中的电子邮件不起作用

时间:2015-08-28 14:05:14

标签: php mysql

我做了一个注册表,但我有一个问题。 email_exist函数不起作用,它不会检查该电子邮件是否已存在于数据库中且没有错误。

有人能帮助我吗?

由于

这是我的代码: PHP代码:
 

    function sanitize ($data1){
        return htmlentities(strip_tags(mysql_real_escape_string($data1)));
    }

    function email_exist($email){
        $email = sanitize($email);
        return (mysql_result(mysql_query("SELECT COUNT(`id`) FROM `companys` WHERE `email` = '$email'"), 0) === 1) ? true : false;
    }

    function output_errors($errors){
        return '<ul class="err_ul"><li>'.implode('</li><li>',$errors).'</li></ul>';
    }

    function array_sanitize(&$item){
        $item = htmlentities(strip_tags(mysql_real_escape_string($item)));
    }

    function register_user($register_data){
        array_walk($register_data, 'array_sanitize');
        $register_data['password'] = md5($register_data['password']);
            $fields = '`' . implode ('`, `', array_keys($register_data)) . '`';
            $data = '\'' . implode('\',\'',$register_data) . '\'';
        mysql_query("INSERT INTO `companys` ($fields) VALUES ($data)");

        require_once 'swiftmailer/lib/swift_required.php';

        $transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl")
            ->setUsername('')
            ->setPassword('');

        $mailer = Swift_Mailer::newInstance($transport);

        $message = Swift_Message::newInstance('Aktivacija korisničkog računa')
            ->setFrom(array(''))
            ->setTo(array($register_data['email']))
            ->setBody("Zdravo".$register_data['name'].",\n\n da bi aktivirao svoj korisnički račun odi na dolje navedeni link:\n\n http://localhost/croglas/activate.php?email=".$register_data['email']."&email_code=".$register_data['email_code']."\n\n crOglas");

        $result = $mailer->send($message);  
    }

    if(empty($_POST) === false){
        $required_fields = array ('company_name', 'director_name', 'director_surname', 'address', 'city', 'zip', 'email', 'phone_number', 'password', 'repassword', 'terms');
        foreach($_POST as $key=>$value){
            if(empty($value) && in_array($key, $required_fields) === true){
                $errors[] = 'Unesite sve podatke';
                break 1;
            }
        }
        if(empty($errors) === true){
            if(strlen($_POST['password']) < 4){
                $errors[] = 'Lozinka mora sadržavati više od 4 znaka';
            }
            if($_POST['password'] !== $_POST['repassword']){
                $errors[] = 'Lozinke se ne poklapaju';
            }
            if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false){
                $errors[] = 'Unijeli ste nevažeću email adresu';
            }
            if(email_exist($_POST['email']) === true){
                $errors[] = 'Ta email adresa je već u uporabi';
            }
        }
    }

    if(empty($_POST) === false && empty($errors) === true){
        $register_data = array(
            'name' => $_POST['company_name'],
            'owner_name' => $_POST['director_name'],
            'owner_surname' => $_POST['director_surname'],
            'address' => $_POST['address'],
            'city' => $_POST['city'],
            'zip' => $_POST['zip'],
            'email' => $_POST['email'],
            'email_code' => md5($_POST['username'] + microtime()),
            'phone_number' => $_POST['phone_number'],
            'password' => $_POST['password'],
            'date_registered' => date("Y-m-d")
        );

        register_user($register_data);
        header('Location: index.php?success');
    }else if(empty($errors) === false){
        echo output_errors($errors);
    }
?>


HTML代码:

<form action="" method="post">
                <ul id="reg_form">
                    Ime tvrtke: <li><input type="text" name="company_name" id="company_name"/></li>
                    Ime vlasnika: <li><input type="text" name="director_name" id="director_name"/>*</li>
                    Prezime vlasnika: <li><input type="text" name="director_surname" id="director_surname"/>*</li>
                    Adresa: <li><input type="text" name="address" id="address"/></li>
                    Grad: <li><input type="text" name="city" id="city"/></li>
                    Poštanski broj: <li><input type="text" name="zip" id="zip"/></li>           
                    E-mail:<li><input type="email" name="email" id="email" class="input"/></li><br>
                    Mobitel/telefon:<li><input type="tel" name="phone_number" id="phone_number" class="input"/></li><br>
                    Zaporka:<li><input type="password" name="password" id="password" class="input"/>*<div class="password_strength" id="password_strength"></div></li><br>
                    Ponovite zaporku:<li><input type="password" name ="repassword" id="repassword" class="input" onkeyup="checkPass(); return false;"/>*</li><br>
                    <li><input type="checkbox" name="terms" id="terms"/>Prihvaćam uvjete korištenja</li><br>                
                    <li><input type="submit" name="reg" id="reg"/></li> 
                </ul>   
</form> 

1 个答案:

答案 0 :(得分:0)

问题已解决

if(email_exist($_POST['email']) === true){
                $errors[] = 'Ta email adresa je već u uporabi';
}

替换为

if(email_exist($_POST['email']) == true){
                $errors[] = 'Ta email adresa je već u uporabi';
}

感谢您的帮助