2014-10-20 64 views
0

我一直在試圖弄清楚這已超過兩天,我正在遵循一個YouTube教程,基本登錄我的Android應用程序,但在此之前我想測試.php腳本。Php,MySql和Webform

我在想,我應該得到的成功,當我按下登錄按鈕,但我得到證書無效,我知道用戶名和密碼是正確的

下面是我的login.php腳本。

require("config.inc.php"); 

if (!empty($_POST)) { 
    //gets user's info based off of a username. 
    $query = "SELECT id, username, passwrd 
      FROM application_users 
      WHERE 
       username = :username 
     "; 
    $query_params = array(
     ':username' => $_POST['username'] 
    ); 

    try { 
     $stmt = $db->prepare($query); 
     $result = $stmt->execute($query_params); 
    } 
    catch (PDOException $ex) { 
     // For testing, you could use a die and message. 
     //die("Failed to run query: " . $ex->getMessage()); 

     //or just use this use this one to product JSON data: 
     $response["success"] = 0; 
     $response["message"] = "Database Error1. Please Try Again!"; 
     die(json_encode($response)); 

    } 

    //This will be the variable to determine whether or not the user's information is correct. 
    //we initialize it as false. 
    $validated_info = false; 

    //fetching all the rows from the query 
    $row = $stmt->fetch(); 
    echo $row; 
    if ($row) { 
     //if we encrypted the password, we would unencrypt it here, but in our case we just 
     //compare the two passwords 
     if ($_POST['password'] === $row['password']) { 
      $login_ok = true; 
     } 
    } 

    // If the user logged in successfully, then we send them to the private members-only page 
    // Otherwise, we display a login failed message and show the login form again 
    if ($login_ok) { 
     $response["success"] = 1; 
     $response["message"] = "Login successful!"; 
     die(json_encode($response)); 
    } else { 
     $response["success"] = 0; 
     $response["message"] = "Invalid Credentials!"; 
     die(json_encode($response)); 
    } 
} else { 
?> 
    <h1>Login</h1> 
    <form action="login.php" method="post"> 
     Username:<br /> 
     <input type="text" name="username" placeholder="username" /> 
     <br /><br /> 
     Password:<br /> 
     <input type="password" name="password" placeholder="password" value="" /> 
     <br /><br /> 
     <input type="submit" value="Login" /> 
    </form> 
    <a href="register.php">Register</a> 
    </form> 
<?php 

} ?>

所以當腳本加載和我輸入從遠程MySQL服務器的值,消息回來爲無效credentials.I只是想確保我的登錄成功之前我回頭看看android部分,這本身就是一個大問題。

+0

您確定數據是通過發佈後操作發送的嗎? – Barry 2014-10-20 16:51:34

+1

完成任何基本的調試? 'var_dump($ _ POST)'和'var_dump($ row)'會向你顯示你的表單和數據庫接受的是什麼樣的php。檢查字符串長度,檢查字符串內容等... – 2014-10-20 16:52:57

+0

從來沒有真正做過任何使用PHP的調試,我應該在哪裏放置var_dump satements。 – 2014-10-20 16:55:46

回答

1

我還沒有機會用真正的數據庫進行測試,但這應該起作用。您仍然需要在文件頂部添加require("config.inc.php");,並添加了自定義數據庫連接。我也和PDO一起工作,所以查詢看起來可能與您目前使用的不同。

<?php 
// Database connection 
try 
{ 
    $db = new PDO('mysql:host=localhost;dbname=mydatabase', 'myusername', 'mypassword'); 
    $db->exec('SET CHARACTER SET UTF8'); 
} 
catch (Exception $e) 
{ 
    //Message in case of error when connecting to the database 
    die('Erreur : ' . $e->getMessage()); 
} 
// *** End database connection 




$username = ""; // Initialize value in order to keep its value so the user can still see it in his form 

if (isset($_POST['login'])) { // if the "login" button is pressed 
    $username = $_POST['username']; // retrieve username value from the form 
    $password = $_POST['password']; // retrieve password value from the form 

    /* 
    * If a username is unique then a way to do it is to count how many times 
    * the couple with this username and this password appears in our database. 
    */ 
    $query = $db->prepare("SELECT COUNT(*) userAmount ". 
          "FROM application_users ". 
          "WHERE username = $username ". 
          "AND password = $password;"); 

    $query->execute(); 
    $query->closeCursor(); 

    $resultAmount = $query->fetch(); 
    if ($resultAmount['userAmount'] == 0){ // If the couple username-password is unfound 
     $message = "Username or password unknown"; 
    } else { 
     $message("Login successful"); 
    } 
} 
?> 


<h1>Login</h1> 
<form action="login.php" method="post"> 
    Username:<br /> 
    <input type="text" name="username" placeholder="username" value="<?php echo($username); ?>" /> 
    <br/><br/> 
    Password:<br/> 
    <input type="password" name="password" placeholder="password" value="" /> 
    <br/><br/> 
    <input type="submit" name="login" value="Login" /> 
<a href="register.php">Register</a> 
</form>