如何防止php / mysql中重复的用户名和电子邮件数据

时间:2016-09-05 18:18:18

标签: php mysql

如何防止重复的用户名和电子邮件数据。

我一直在制作登录/注册系统,我正在接近完成我的注册部分代码。我遇到的唯一问题是如何使用它,以便用户无法注册重复的用户名和电子邮件。我希望它能够工作,以便我的数据库不会接受这些信息,它会告诉用户有关错误的信息。任何帮助表示赞赏。

这是我的源代码:

=============================================== ==================

    <?php
    session_start();
    include 'loginlogoutregister.php';
    //connecting to database
    $link = mysqli_connect("localhost","root","","authentication");

    if(isset($_POST['Login_Btn'])){
        login($link);
    }
    if(isset($_POST['Register_Btn'])){
        register($link);
    }
    if(isset($_POST['Logout_Btn'])){
        logout($link);
    }
?>
<html>
<head>
    <Title>LogIn</Title>
    <link rel="stylesheet" type="text/css"  href="style.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script>
        var modal = document.getElementById('PopUpLR');
        var btn = document.getElementById("myBtn");
        var span = document.getElementsByClassName("closeLR")[0];
        btn.onclick = function() {
            modal.style.display = "block";
        }
        span.onclick = function() {
            modal.style.display = "none";
        }
        window.onclick = function(event) {
            if (event.target == modal) {
                modal.style.display = "none";
            }
        }
    </script>
</head>
<body>
<div class = "headerContainer">
    <div class = "headerLeft">
        test
    </div>
    <div class="headerMiddle"> 
        <h1>Website</h1>
        <div><h4>
        <?php 
            if(isset($_SESSION['username']) ){
                echo "Welcome ".$_SESSION['username'];

            }else{
                echo "Logged Out ";
            }
        ?>
        </h4></div>
    </div>
    <div class = "headerRight">
        <div class = "loginRegister">
            <p>Login/Register</p>
        </div>
    </div>
</div>
<button id="myBtn">Open Modal</button>
<div id = "PopUpLR">
    <div class = "PopUpLRContent">
        <span class="closeLR">x</span>
        <p>Some text in the Modal..</p>
    </div>
</div>
</body>
</html>



 #PopUpLR{
    display: none; /* Hidden by default */
    position: fixed; /* Stay in place */
    z-index: 1; /* Sit on top */
    left: 0;
    top: 0;
    width: 100%; /* Full width */
    height: 100%; /* Full height */
    overflow: auto; /* Enable scroll if needed */
    background-color: rgb(0,0,0); /* Fallback color */
    background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}
.PopUpLRContent{
    background-color: #fefefe;
    margin: 15% auto; /* 15% from the top and centered */
    padding: 20px;
    border: 1px solid #888;
    width: 80%; /* Could be more or less, depending on screen size */
}
.closeLR {
    color: #aaa;
    float: right;
    font-size: 28px;
    font-weight: bold;
}

.closeLR:hover,
.closeLR:focus {
    color: black;
    text-decoration: none;
    cursor: pointer;
}

2 个答案:

答案 0 :(得分:1)

UNIQUE CONSTRAINT表格的email_userusername列上创建tb_user;如果您尝试插入重复项,则会抛出错误。

ALTER TABLE tb_user ADD CONSTRAINT constr1 UNIQUE (email_user);

答案 1 :(得分:-1)

您可以在$q = $op->save()之前使用帖子中的名称和密码组合进行SELECT查询。

如果它返回任何结果,则返回一条消息并阻止保存。