我剛剛開始搞亂php,我正在測試我的新知識,嘗試使用數據庫來存儲用戶名和密碼的簡單登錄頁面。一切進展順利,但我跑進了一堵巨大的牆壁!出於某種原因,我的獲取方法$result = $stmt->fetch();
沒有返回任何結果,即使如果我在phpmyadmin中運行sql查詢,它會返回一行就好!這與我從表單或加密獲取輸入的方式有關嗎?通過php和sql檢查賬戶詳細信息
這是我的完整代碼。謝謝你們要溫柔我是全新的,以該哈哈
<?php
include 'inc/db.inc.php';
$link = new PDO(DB_INFO, DB_USER, DB_PASS);
?>
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7" lang="en"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8" lang="en"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9" lang="en"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="css/style.css">
<script src="js/libs/modernizr-2.5.3.min.js"></script>
</head>
<body>
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['register'])) {
$sql = "INSERT INTO account (account_name, account_pass) VALUES (:name, :pass)";
$stmt = $link->prepare($sql);
$stmt->bindParam(':name', $name);
$stmt->bindParam(':pass', $pass);
$name = $_POST["username"];
$pass = md5($_POST["password"]);
$stmt->execute();
echo "Thank you for registering!";
} else if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['login'])) {
$sql = "SELECT account_name, account_pass FROM account WHERE account_name=:name AND account_pass=:pass";
$stmt = $link->prepare($sql);
$stmt->bindParam(':name', $name, PDO::PARAM_STR);
$stmt->bindParam(':pass', $pass, PDO::PARAM_STR);
$pass = strip_tags(md5($_POST["password"]));
$name = strip_tags($_POST["username"]);
$stmt->execute();
$result = $stmt->fetch();
print_r($result);
if(empty($name) || empty($pass)){
echo "please enter a username and password!";
} else {
if(empty($result)){
echo "yes";
} else {
echo "no";
}
}
} else {
?>
<form method="post" action="test.php">
<label for="username">Username: </label>
<input type="text" name="username" />
<label for="password">Password: </label>
<input type="password" name="password" />
<input type="submit" name="login" value="Login" />
<input type="submit" name="register" value="register" />
</form>
<?php } ?>
</body>
</html>
MySQL的,還是什麼......? – Fallenreaper
你可以在'fetch()'附加一點調試嗎?當你做SELECT時,很高興看到數據庫返回的內容 – andrewsi