본문 바로가기
MySql

MySQL 사용자 이름이 데이터베이스에 있는지 PHP 확인

by 베이스 공부 2020. 12. 25.
반응형

그래서 저는 사용자 양식 등록을하고 있습니다. 사용자가 이메일을 입력하는 동안 웹 사이트는 이메일이 이미 사용되었는지 여부를 확인하기 전에 데이터베이스를 확인합니다. 등록 버튼을 누르십시오.

내가 가진 문제는 확인하지 않는다는 것입니다. "데이터베이스에서 검색 중" 만 표시됩니다. 나는 누군가가 내가 만드는 오류를 잡을 수 있도록 내 코드를 게시하고 싶습니다.

이것은 내 등록 페이지의 일부입니다.

<tr class = "spacearound"> <!-- input for email address -->
    <th> &emsp;Email: </th>
    <td>
        <input type = "text" id = "user_email" size = "50"
            maxlength = "50" name = "u_email" 
            title = "Enter your email please" 
            onchange = "EmailCheck();" 
            onkeypress = "return InputLimiter(event, 'emailCharacters');"
            /> &#42;
        <span id = "email_status"> </span>
    </td>
    <td><?php  echo $message; ?></td>
</tr>

이것은 내 JavaScript 파일 인 "checkusers.js"입니다.

$('#user_email').keyup(function() {
    var username = $(this).val();

    $('#email_status').text('Searching database.');

    if(username != ''){
        $.post('checkemail.php',{ username: username }, function(data) {
            $('#email_status').text(data);
        });
    } else {
        $('#email_status').text('');
    }

});

그리고 이것은 내 php 파일입니다. 여기에서 이메일 "checkemail.php"를 확인합니다.

<?php
    define('dbHost', 'xxxxx');
    define('dbUser', 'xxxxx');
    define('dbPassword', 'xxxxx');
    define('dbName', 'xxxxx');
    error_reporting(E_ALL ^ E_NOTICE);

    $db = mysqli_connect(dbHost, dbUser, dbPassword, dbName);

    if(mysqli_connect_errno()) { //if connection database fails
        echo("Connection not established ");
    }  //by now we have connection to the database

    if(isset($_POST))['username'])){ //if we get the name succesfully
    $username = mysqli_real_escape_string($db, $_POST['username']);
        if (!empty($username)) {
            $username_query = mysqli_query($db, "SELECT COUNT(`firstName`) FROM `users` WHERE `email`='$username'");    

        $username_result = mysqli_fetch_row($username_query);

            if ($username_result[0] == '0') {
                echo 'Email available!';
            } else {
                echo 'Sorry, the email '.$username.' is taken.';
            }
        }
    }

?>

 

해결 방법

 

여기에 오류가 있습니다

if(isset($_POST))['username'])){

그것은해야한다

if(isset($_POST['username'])){

$ _ POST [ 'username'] 은 isset 함수 안에 포함되어야합니다.

 

참조 페이지 https://stackoverflow.com/questions/18244863

 

 

반응형

댓글