我想创建一个工作登录表单.这是我所做的,这个显示不能选择db.编辑了login.php文件?phperror_reporting(E_ALL);//Connection Variables:$dbhost = localhost;$dbname = ;$dbuser = ;$dbpass = ;try{//Conn...
我想创建一个工作登录表单.这是我所做的,这个显示不能选择db.
编辑了login.php文件
<?php
error_reporting(E_ALL);
//Connection Variables:
$dbhost = "localhost";
$dbname = "";
$dbuser = "";
$dbpass = "";
try{
//Connection to SQL:
$conn = new PDO("mysql:host=$dbhost; dbname=$dbname", $dbuser, $dbpass);
//Error messagin enabled:
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e)
{
echo $e->getMessage();
}
$user = '';
$pass = '';
$sum = 0;
$error_msg = "Please type a username and a password";
if(isset($_POST['login']))
{
//Start a session:
session_start();
$user = $_POST['email'];
$pass = $_POST['password'];
if(empty($user) && empty($pass))
{
echo $error_msg;
$pass = '';
}
if(empty($user) || empty($pass))
{
echo $error_msg;
$user = '';
$pass = '';
}
if(!empty($user) && !empty($pass))
{
//SQL:
$query = $conn->prepare("SELECT * FROM login WHERE user = :u AND password= :p LIMIT 1");
$query->bindParam(":u", $user);
$query->bindParam(":p", $pass);
//Execute query:
$query->execute();
$number_rows = $query->fetch(PDO::FETCH_NUM);
if($number_rows>0)
{
echo $user;
$_SESSION['usern'] = $user;
$_SESSION['passw'] = $pass;
header("Location: ./pages/home.php");
}
//echo $user;
else
{
echo "Invalid username or password";
header("Location: index.html");
}
}
}
if(!isset($_POST['login']))
{
echo "Login button not clicked";
}
?>
我读了越来越多关于此的文章,但我找不到解决方案.
编辑过的HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="../../favicon.ico">
<title>Signin for OTMS</title>
<!-- Bootstrap core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="signin.css" rel="stylesheet">
<!-- Just for debugging purposes. Don't actually copy these 2 lines! -->
<!--[if lt IE 9]><script src="../../assets/js/ie8-responsive-file-warning.js"></script><![endif]-->
<script src="js/ie-emulation-modes-warning.js"></script>
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="container">
<form action="login.php" method="post" class="form-signin">
<h2 class="form-signin-heading">Please sign in</h2>
<label for="inputEmail" class="sr-only">Email address</label>
<input type="email" name="email" id="inputEmail" class="form-control" placeholder="Email address" required autofocus>
<label for="inputPassword" class="sr-only">Password</label>
<input type="password" name="password" id="inputPassword" class="form-control" placeholder="Password" required>
<div class="checkbox">
<label>
<input type="checkbox" value="remember-me"> Remember me
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit" name="login">Sign in</button>
</form>
</div> <!-- /container -->
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<script src="assets/js/ie10-viewport-bug-workaround.js"></script>
</body>
</html>
请帮我找出错误是什么.
我使用phpMyAdmin创建了我的数据库,它在localhost中.我使用Bootstrap设计的接口.
这是我现在得到的错误:
database name- otmsdb
table name- login
email, passowrd,
button name- login
解决方法:
先生,您的代码很容易受到SQL注入的攻击.请开始使用MySQLi或PDO.这是一个用于登录的PDO代码,应该可以正常使用:
资料来源:Udemy Online课程.
编辑:使用此代码,并将变量更改为您的
<?php
if(isset($_POST['login'])){
session_start();
$errmsg_arr = array();
$errflag = false;
// configuration
$dbhost = "localhost";
$dbname = "your database name";
$dbuser = "your username";
$dbpass = "your password";
// database connection
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->exec("SET CHARACTER SET utf8mb4");
// new data
$user = $_POST['your name of email input'];
$password = $_POST['password'];
if($user == '') {
$errmsg_arr[] = 'You must enter your Username';
$errflag = true;
}
if($password == '') {
$errmsg_arr[] = 'You must enter your Password';
$errflag = true;
}
// query
$result = $conn->prepare("SELECT * FROM login WHERE username= :u AND password= :p");
$result->bindParam(':u', $user);
$result->bindParam(':p', $password);
$result->execute();
$rows = $result->fetch(PDO::FETCH_NUM);
if($rows > 0) {
$_SESSION['username'] = $user;
header("location: ./pages/home.php");
}
else{
$errmsg_arr[] = 'Username and Password are not found';
$errflag = true;
}
}
?>
HTML表格:
<body>
<form action="" method="post" name="login">
<input type="text" name="username" placeholder="Enter a Username"/>
<input type="password" name="password" placeholder="***"/>
<input type="submit" name="login_submit" value="Login"/>
</form>
</body>
沃梦达教程
本文标题为:使用php,mysql和html创建登录表单
基础教程推荐
猜你喜欢
- Jquery Ajax请求方法小结(值得收藏) 2022-10-17
- 基于bootstrap的上传插件fileinput实现ajax异步上传功能(支持多文件上传预览拖拽) 2023-02-01
- 《CSS3实战》笔记--渐变设计(二) 2022-11-13
- 初学者必看的Ajax总结篇 2023-01-26
- 智能应用横幅;适用于Android / Google Play的Windows应用商店HTML元标记? 2023-10-25
- 开发效率翻倍的Web API使用技巧 2023-07-09
- React Hooks 实现的中文输入组件 2023-07-10
- Ajax发送和接收二进制字节流数据的方法 2022-12-28
- Ajax 高级功能之ajax向服务器发送数据 2023-01-21
- 不使用XMLHttpRequest对象实现Ajax效果的方法小结 2023-02-23