PHP菜鸟入门之简单的登录注册(增删改查)
刚入门的时候做的,很多东西还不会就做的麻烦了些,样式简洁
功能:登录注册注销。登陆成功则显示信息,登录失败则提示错误。
登录页面login.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
div {
width: 400px;
margin: auto;
border:1px solid black;
padding-left: 150px;
}
input {
margin-top: 3px;
margin-bottom: 3px;
}
form {
margin-bottom: 20px;
}
</style>
</head>
<body>
<h3>
<center>
please login
</center>
</h3>
<div>
<form method="post" action="login.php">
name:
<input type="text" name="name" value="" required />
<br />
password:
<input type="password" name="password" value="" required />
<br />
<input type="submit" value="submit" />
</form>
<a href="register.php">还没有账号?点此处注册</a>
</div>
</body>
</html>
注册页面(有用户名的正则判断)register.php
<?php
/*引入连接数据库文件*/
include '../db/connection.php';
try {
$name = $password = "";
$name_err = $password_err = "";
$name_chech = $password_check = $result_text = "";
function testInput($_data)
{
/*去掉字符串两侧空白字符或其他预定义字符*/
$_data = trim($_data);
/*刪除反斜杠*/
$_data = stripslashes($_data);
/*把预定义的字符转换为HTML实体*/
$_data = htmlspecialchars($_data);
return $_data;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
/*验证名字,仅允许a-zA-Z(1-8)*/
if (empty($_POST["name"])) {
$name_err = "name is required";
throw new Exception("please input your name");
} else {
$name = $_POST["name"];
if (!preg_match("/^[a-zA-Z]{1,8}$/", $name)) {
$name_err = "Only letters can be allowed(1-8)";
throw new Exception("please input your name correctly");
} else {
$name_chech = true;
}
}
/*密码不可以为空*/
if (empty($_POST["password"])) {
$password_err = "password is required";
throw new Exception("please input your password");
} else {
$password = testInput($_POST["password"]);
$password_check = true;
}
/*如果验证成功则添加到数据库*/
if ($name_chech && $password_check) {
$udbname = "test";
$uname = $_POST["name"];
$upassword = $_POST["password"];
$sql = 'insert into test.user values("' . $uname . '","' . $upassword . '");';
$result = sqlExec($udbname, $sql);
if ($result == 1) {
$result_text = "注册成功";
} else {
$result_text = "注册失败";
}
}
}
} catch (Exception $e) {
echo $e->getMessage();
}
?>
<h3>
<center>please input your message</center>
</h3>
<div>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
name:
<input type="text" name="name" value="" />
<span class="error"> *<?php echo $name_err; ?></span>
<br />
password:
<input type="password" name="password" value="" />
<span class="error"> *<?php echo $password_err; ?></span>
<br />
<input type="submit" value="submit" />
<a href="login.html">去登陆</a>
<p>
<?php echo $result_text; ?>
</p>
</form>
</div>
主页面(登录之后的页面无论是否成功都跳转到这个页面)login.php
<?php
/*引入文件*/
include '../db/connection.php';
try {
/*如果登录成功则显示名字,登录不成功则显示无此用户*/
$name = $_POST["name"];
$password = $_POST["password"];
$sql = "select count(uname) from test.user where uname ='" . $name . "' and upassword ='" . $password . "';";
/*调用PDO实现查找,查找数据库中是否有该用户,若有密码是否正确*/
/*test:数据库的名字*/
$result = sqlQuery("test", $sql);
$display = $result_text = $password = $search_user = $emp = $score_max = $score_min = "";
/*遍历数据库*/
foreach ($result as $key => $value) {
$search_user = $value["count(uname)"];
}
/*数据库找到了此人*/
if ($search_user == 1) {
$display = true;
/*欢迎*/
$result_text = "welcome " . $_POST["name"] . "!!! 请选择您要进行的操作(修改/注銷)";
echo "<br><br>";
/*emp,score两表联查*/
$sql_emp = "select e.*,s.* from emp e,score s where e.uname='" . $search_name . "' and e.uname = s.uname;";
$emp = sqlQuery('test', $sql_emp);
$sql_max = 'select max(math) from test.score;';
$score_max = sqlQuery('test', $sql_max);
$sql_min = 'select min(chinese) from test.score;';
$score_min = sqlQuery('test', $sql_min);
} else {
/*用户不存在*/
$display = false;
$result_text = "please check your username and password";
throw new Exception('your password or username is wrong');
}
} catch (Exception $e) {
echo $e->getMessage();
}
?>
<div>
<?php
echo $result_text;
?>
</div>
<div id="main">
<form method="post" action="modify.php">
name:
<input type="text" name="name" value="<?php echo $_POST["name"]; ?>" placeholder="<?php echo $_POST["name"]; ?>" readonly />
<br />
password:
<input type="password" name="password" value="" placeholder="" required />
<br />
<input type="submit" value="修改" />(修改成功会直接前往登录主页!)
</form>
<a href="delete.php?name=<?php echo $name; ?>">注销</a>
<a href="login.html">回到登录页面</a>
<br>
</div>
<table cellpadding="0" cellspacing="0" id="table">
<tr>
<td>name</td>
<td>gender</td>
<td>dept</td>
<td>math_score</td>
<td>chinese_score</td>
<td>total_score</td>
<td>average_score</td>
</tr>
<?php
/*遍历数据库中emp表的数据*/
if ($emp == "" || $emp == null) {
echo "you can not do anything";
} else {
foreach ($emp as $key => $value) {
$total_score = $value["chinese"] + $value["math"];
$average_score = $total_score / 2;
echo "<tr>";
echo "<td>" . $value["uname"] . "</td>";
echo "<td>" . $value["gender"] . "</td>";
echo "<td>" . $value["dept"] . "</td>";
echo "<td>" . $value["math"] . "</td>";
echo "<td>" . $value["chinese"] . "</td>";
echo "<td>" . $total_score . "</td>";
echo "<td>" . $average_score . "</td>";
}
foreach ($score_max as $key => $value) {
echo "数学最高分是:";
echo $value["max(math)"];
}
foreach ($score_min as $key => $value) {
echo "语文最低分是";
echo $value["min(chinese)"];
}
}
?>
</table>
<script>
var table = document.getElementById("table");
var main = document.getElementById("main");
var boo = <?php echo $display; ?>;
if (boo == "1") {
table.style.display = "block";
main.style.display = "block";
}
</script>
删除方法:delete.php
<?php
/*导入文件*/
include '../db/connection.php';
try {
$name = $_GET["name"];
$sql = 'delete from test.user where uname="' . $name . '";';
/*调用PDO方法实现删除*/
$result = sqlExec("test", $sql);
if ($result == 1) {
throw new Exception("注销成功");
} else {
throw new Exception("注销失败");
}
} catch (Exception $e) {
echo $e->getMessage();
}
echo "<br>";
echo "<a href = 'login.html'>点击前往登录页面</a>";
echo "<br>";
echo "<a href = 'register.php'>点击前往注册页面</a>";
?>
修改方法modify.php
<?php
/*导入文件*/
include '../db/connection.php';
try {
$name = $_POST["name"];
$password = $_POST["password"];
$sql = 'update test.user set upassword="' . $password . '" where uname="' . $name . '";';
/*调用PDO实现用户密码的修改*/
$result = sqlExec("test", $sql);
if ($result == 1) {
header("Location:login.html");
} else {
throw new Exception("修改失败");
}
} catch (Exception $e) {
$e->getMessage();
}
?>
错误页面error.php
<html>
<head>
<body>
<form method="post" action="test2.php">
name:
<input type="text" name="name" value="" />
<input type="submit" value="submit" />
</form>
</body>
</head>
连接数据库方法 connection.php(我放在了db文件夹下面,注意路径)
<?php
function dbConn($udb_name)
{
try {
//数据库链接
$dbms = 'mysql';
//数据库类型
$host = 'xxx';
//数据库的链接用户名
$user = 'xxx';
//对应的密码
$pass = 'xxx';
$dsn = "$dbms:host=$host;dbname=$udb_name";
//初始化一个PDO对象
$dbh = new PDO($dsn, $user, $pass, array(PDO::MYSQL_ATTR_INIT_COMMAND => "set names utf8"));
} catch (PDOException $e) {
die("Error:" . $e->getMessage() . "<br />");
}
return $dbh;
}
function sqlQuery($udb_name, $sql)
{
//初始化一个PDO对象
$dbh = dbConn($udb_name);
$shou = $dbh->query($sql);
return $shou;
}
function sqlExec($udb_name, $sql)
{
//初始化一个PDO对象
$dbh = dbConn($udb_name);
$num = $dbh->exec($sql);
return $num;
}
更多推荐
PHP菜鸟入门之简单的登录注册
发布评论