如何防止用户从PHP应用程序向SQL Server插入重复的电子邮件?

时间:2016-10-20 17:33:31

标签: php html sql-server

代码验证电子邮件是否存在,但它不会阻止在表格中插入重复项。我想插入,如果它不存在,并重定向到thank_you.php - 代码看起来像这样:

<?php
if (isset($_POST['submit'])) {
    $firstName = $_POST['firstName'];
    $lastName = $_POST['lastName'];
    $username = $_POST['username'];
    $password= password_hash($_POST['password'], PASSWORD_DEFAULT);
    $country = $_POST['country'];
    $city = $_POST['city'];
    $state = $_POST['state'];
    $phone = $_POST['phone'];

    include 'connect_sql.php';

        $sql = "INSERT INTO [Sonic].[dbo].[member] ([firstName],[lastName],[UserName],[Password],[Country],[City],[State],[Phone])
        VALUES ('$firstName', '$lastName', '$username', '$password', '$country', '$city', '$state', '$phone')";

        $email = "SELECT UserName FROM [Sonic].[dbo].[member] WHERE UserName='$username'";

        $row = sqlsrv_fetch_array( $email, SQLSRV_FETCH_ASSOC);

        $stmt = sqlsrv_query( $conn, $sql );

        if( $row === false) {                 
        echo "<div id='message'> Username $username already exist</div>"; 
        }           
        else {          
        header('Location: thank_you.php');
        }
     }
?>

0 个答案:

没有答案