PHP development corporate website tutorial - adding administrators
In the previous section we have taken out the information from the database and displayed the display page
As you can see from the code in the previous section, we click to add an administrator and submit it to the addu.php page
Let’s take a look at the code of this page
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>添加管理员</title> <style type="text/css"> .ipt{width:180px;height:30px;border-radius:5px; outline:none;border:1px solid #eee;box-sizing:border-box;padding-left:15px;} .sub{width:50px;height:20px;border:1px solid #eee;background:#eee;color:#ff7575;} </style> </head> <body> <form method="post" action="adduser.php"> 用户名:<input type="username" name="username" class="ipt"></br></br> 密 码:<input type="password" name="password" class="ipt"></br></br> <input type="submit" value="添加" class="sub"> </form> </body> </html>
Looking at the above code, we can see that the form is submitted to the adduser.php file in the form of post
Let’s do this Take a look at the code of the adduser.php file, and then analyze the code:
<?php //添加管理员部分代码,注意,当数据库存在该管理员账户时,不允许添加 require_once('conn.php'); $name = $_POST['username']; $password = md5($_POST['password']); $sql1 = "select * from user where username ='$name'"; $info = mysql_query($sql1); $res1 = mysql_num_rows($info); if($res1){ echo "<script>alert('管理员已存在');location.href='addu.php';</script>"; }else{ $sql = "insert into `user`(username,password) values('$name','$password')"; $res = mysql_query($sql); if($res){ echo "<script>alert('添加管理员成功');location.href='user.php';</script>"; }else{ echo "<script>alert('添加管理员失败');history.go(-1);</script>"; } } ?>
First we also need to connect to the database, and then receive the information submitted by the form
After that we need to judge the form submission Does the user exist? If it exists, a prompt will be given. If it does not exist, you can add it
As shown in the above code, in this way we have completed the function added by the administrator