2014-12-03 5 views
0

'로그인'을 클릭하면 데이터베이스에 사용자가 있는지 확인하고 싶습니다. 그렇다면 "사용자 존재"를 표시하고 그렇지 않으면 "사용자 존재하지 않음"을 표시합니다. 이 일에 어떻게 가야합니까? 지금까지 내가 가지고 :SQL 데이터베이스에 사용자가 있는지 확인하는 방법

내 로그인에
<?php 
$servername = "localhost"; 
$username = "root"; 
$password = "root"; 
$dbname = "test"; 

// Create connection 
$conn = new mysqli($servername, $username, $password, $dbname); 
// Check connection 
if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 

?> 

<!DOCTYPE html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Test</title> 
</head> 

<body> 
<form method="post" action="login.php"> 
Username: <input type="text" value="" name="username" /><br /><br /> 
Password: <input type="password" value="" name="password" /><br /><br /> 
<input type="submit" name="submit" value="Log In"/> 
</form> 
</body> 
</html> 
+0

힌트 :'SELECT COUNT (*) ... ' – zerkms

답변

1

/나는이의 라인을 따라 뭔가를 사용했다 스크립트를 등록 ..

// check existing username 
$prep_stmt = "SELECT id FROM your-table-name WHERE username = ? LIMIT 1"; 
$stmt = $mysqli->prepare($prep_stmt); 

if ($stmt) { 
    $stmt->bind_param('s', $username); 
    $stmt->execute(); 
    $stmt->store_result(); 

      if ($stmt->num_rows == 1) { 
        // A user with this username already exists 
        $error_msg .= '<p class="error">A user with this username already exists</p>'; 
        $stmt->close(); 
      } 
      $stmt->close(); 
    } else { 
      $error_msg .= '<p class="error">Database error line 55</p>'; 
      $stmt->close(); 
    } 
+0

mysqli 준비 함수를 추가하고 실행해야합니다 :) – DarkBee

+0

@DarkBee OP에 대한 숙제 일 수도 있습니다 :-) 심지어 초보자도 쿼리가 너무 많습니다 – zerkms

+0

공정한 시점이지만 여전히 – DarkBee

2

환영이 하나

session_start(); 
if($_SERVER['REQUEST_METHOD'] === 'POST'){ 

    $servername = "localhost"; 
    $username = "root"; 
    $password = "root"; 
    $dbname = "test"; 

// Create connection 
    $conn = new mysqli($servername, $username, $password, $dbname); 
// Check connection 
    if ($conn->connect_error) { 
     die("Connection failed: " . $conn->connect_error); 
    } 

    $user = $conn->real_escape_string(htmlspecialchars(trim($_POST['username']))); 
    $query = "SELECT `username` FROM `users` WHERE `username` = '$user'"; 

    $result = $conn->query($query); 
    if($result->num_rows > 0) { 
     $_SESSION['allowToWelcome'] = true; 
     header('Location:welcome.php'); 
     die(); 
    } 
    else $message = 'user does not exist'; 

} 

?> 

    <!DOCTYPE html> 
    <html> 
     <head> 
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
      <title>Test</title> 
     </head> 

     <body> 
      <form method="post" action="login.php" enctype="multipart/form-data"> 
       <?php if(isset($message)) : ?> 
        <div class="error"><?php echo $message; ?></div> 
       <?php endif; ?> 

       Username: <input type="text" value="" name="username" /><br /><br /> 
       Password: <input type="password" value="" name="password" /><br /><br /> 
       <input type="submit" name="submit" value="Log In"/> 
      </form> 
     </body> 
    </html> 

시도 .php 이것과 같이 확인할 수 있습니다

session_start(); 
if(!isset($_SESSION['allowToWelcome'])){ 
    header('Location:login.php'); 
    die(); 
} 
+0

정말 고마워요! –

+0

나는 '환영 페이지'에 사용자를 데려 가고 싶지 않다면, '존재하지 않는다'라고 표시하고 싶다. 다른 페이지로 어떻게 링크합니까? html, form 액션에서이 작업을 수행해야합니까? –

+0

위 코드를 수정하십시오. 행 헤더를 확인하십시오 ('Location : welcome.php'); welcome.php를 프로젝트의 모든 페이지로 변경할 수 있습니다. – Oleg

관련 문제