1 回答
TA贡献1876条经验 获得超7个赞
你需要连接到页面顶部的数据库(或者更好的是在另一个文件中并需要它)这样你就可以像db.ph那样做某事:
<?php
$host = '';
$dbuser = '';
$paswrd = '';
$dbname = '';
$dns = 'mysql:host=' . $host .'; dbname=' . $dbname;
try
{
$pdo = new PDO($dns, $dbuser, $paswrd);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// echo "polaczono z mysql";
}
catch(PDOException $e)
{
// echo "nie udało się połączenie: " . $e->getMessage();
}
?>
接下来在登录页面你做:
require('db.php');
if(isset( $_POST['login']))
{
require('./config/db.php');
/*
$userName = $_POST['userName'];
$userEmail = $_POST['password'];
*/
$userName = filter_var( $_POST['userName'], FILTER_SANITIZE_STRING);
$userPassword = filter_var( $_POST['userPassword'], FILTER_SANITIZE_STRING);
$stmt = $pdo -> prepare('SELECT * FROM Users WHERE name =?');
$stmt -> execute([$userName]);
$user = $stmt ->fetch();}
你可以检查它是否正确,例如 if($user->password==$userPassword)
在 html 中:
<form action="login.php" method="POST">
<div class="form-group">
<label for="userName">Name</label>
<input required type="text" name="userName" ">
</div>
<div class="form-group">
<label for="userPassword">Password</label>
<input required type="password" name="userPassword"/>
</div>
<button name="login" type="submit" >Login</button>
- 1 回答
- 0 关注
- 134 浏览
添加回答
举报