


Detailed explanation of simple interactive site development with PHP MYSQL
This article mainly introduces PHP MYSQL simple interactive site development in detail, which has certain reference value. Interested friends can refer to it
Purpose: Use PHP and MYSQL to simulate permission management Implementation of the system
The general user can only view other user information and cannot modify, add, and delete operations. The root user can complete the above three operations.
Implementation ideas
1. Create two data tables in the MySQL database. One data table stores usernames and passwords for login verification, and the other stores basic information such as user permissions.
2. When submitting the form to log in, first check whether the user exists in the database. If it does not exist, an error will be reported. If it exists, the password will be verified. If the password is incorrect, an error will be reported. If the password is correct, the login will display all the information stored in the database. User information and current login username.
3. When the user performs add or delete operations, first determine whether the permissions are sufficient. If there is permission, complete the corresponding operation and modify the database content. Otherwise, it will prompt that there is no permission.
Detailed implementation
1. Login page
<center> <form method="post" action="<?php echo $_SERVER['SCRIPT_NAME'] ?>"> 用户名: <input type="text" name="user_name"> 密码: <input type="text" name="password"> <input type="Submit" name="submit" value="登陆"> </form> </center>
The effect is as follows:
2 .Connect to the database to verify the login name and password
//登陆处理 if (isset($_POST['submit'])) { // 用户名输入为空 if($_POST['user_name'] == '') // 调用javascript函数动态提醒 echo "<script type='text/javascript'>dis_alert(\"用户名\",1);</script>";// 密码输入为空 if($_POST['password'] == '') // 调用javascript函数动态提醒 echo "<script type='text/javascript'>dis_alert(\"密码\",1);</script>"; // 用户名与密码均不为空 $user_name = $_POST['user_name']; //链接数据库,从中读出用户名和密码 $db = mysql_connect("localhost", "root", "123456"); mysql_select_db("linyimin", $db); $result = mysql_query("select * from login where user_name = '$user_name'"); $num = mysql_num_rows($result); //判断用户输入的用户名存在,验证密码 if($num != 0) { $user_name = mysql_result($result,0,'user_name'); $password = mysql_result($result,0,'password'); if(strcmp($password,$_POST['password']) != 0) { echo "密码错误"; //密码错误,报错 $password = $_POST['password']; echo "<script type='text/javascript'>dis_alert('密码错误',3);</script>"; } // 密码正确 else { session_unset(); session_start(); $_SESSION['user_name'] = $_POST[user_name]; header("Location:http://localhost/display.php"); exit; } } // 用户输入的用户名不存在,报错 else if($num == 0) { // 用户名不存在,报错 $user_name = $_POST['user_name']; echo "<script type='text/javascript'>dis_alert(\"$user_name\",2);</script>"; } mysql_close($db); }//登陆处理结束
Input error reminder function
<script type="text/javascript"> // 登陆错误提醒 function dis_alert(var1, var2) { // 用户名和密码不能为空提醒 if(var2 == 1) { alert(var1 + " 不能为空,请重新输入"); history.back(-1); } // 用户名不存在错误提醒 if(var2 == 2) { alert("该用户名 " + var1 + " 不存在,请重新输入"); history.back(-1); } // 密码错误提醒 if(var2 == 3) { alert("密码错误,请重新输入"); history.back(-1); } } </script>
Error reminder rendering:
3. After successful login, all user information in the database and the current login user name will be displayed
// 获取登陆名 session_start(); $NAME = $_SESSION['user_name']; // 连接数据库,获取数据并显示 function display() { global $NAME; $db = mysql_connect("localhost", "root", "123456"); mysql_select_db("linyimin",$db); $sql = "select * from admin_info"; $result = mysql_query($sql); // 显示信息表 echo "<h3 align=right color=#FFFFFF> 当前用户:$NAME</h6>"; echo "<table border = 0 align = center width = 1000></br>"; // 添加超链接 echo "<tr align = center><th> <a href =\"display.php?add=yes\">ADD</a></th><br>"; // 修改添加超连接 echo "<th> <a href =\"display.php?update=yes\">UPDATE</a></th><br>"; // 删除超链接 echo "<th> <a href =\"display.php?delete=yes\">DELETE</a></th></tr><br>"; echo "</table>"; echo "<table border = 2 align = center width = 1000></br>"; // 表头 echo "<tr><th colspan=\"3\">管理员权限表</th></tr><br>"; echo "<tr align = center><td>姓名</td><td>权限</td><td>职务</td></tr><br>"; while($row = mysql_fetch_row($result)) { // 显示管理员信息并通过超链接调用处理函数 echo "<tr align = center><td>$row[0]</td>"; echo "<td>$row[1]</td>"; echo "<td>$row[2]</td></tr>"; } echo "</table>"; mysql_close($db); }
The display effect is as follows:
4. Implementation of modification, deletion and addition operations
Modify and add page
<center> <form method="post" action="<?php echo $_SERVER['URL'] ?>"> 姓名: <input type="text" name="user_name"> 权限: <input type="text" name="pemission"> 职务: <input type="text" name="position"> <input type="Submit" name="update" value="提交"> </form> </center>
The effect is as follows:
Delete page
<center> <form method="post" action="<?php echo $_SERVER['URL'] ?>" onsubmit="return confirm('请确认删除');"> 姓名: <input type="text" name="user_name"> <input type="submit" name="update" value="删除"> </center>
The rendering is as follows:
// 调用修改函数 if ($_GET[update]) { modify("update"); } // 调用添加函数 elseif($_GET[add]) { modify("add"); } elseif($_GET[delete]) { modify("delete"); }Implementation of modify() function
// 修改数据函数 /* 点击修改超链接,跳转到修改页面 表单中,名字项指定要修改记录 权限和职务项为可修改内容 */ function modify($operation) { if(isset($_POST['update'])) { // 有root权限修改,修改 if($operation == "update" && judge("update")) { $user_name = $_POST[user_name]; $sql = "UPDATE admin_info SET pemission = '$_POST[pemission]', position ='$_POST[position]' WHERE user_name = '$user_name'"; mysql_query($sql); mysql_close($db); display(); } // 添加 elseif(judge("add") && $operation == "add") { $user_name = $_POST[user_name]; $sql = "insert into admin_info (user_name, pemission, position) values ('$_POST[user_name]','$_POST[pemission]','$_POST[position]')"; mysql_query($sql); mysql_close($db); display(); } // 删除 elseif(judge("delete") && $operation == "delete") { $user_name = $_POST[user_name]; // 获取确认情况 $sql = "delete from admin_info where user_name = \"$user_name\""; mysql_query($sql); } } }Implementation of judge() function
// 判断修改用户名是否存在和该用户是否具有权限 function judge($operation) { global $NAME; // 修改用户名 $user_name = $_POST['user_name']; // 连接数据库,获取数据 $db = mysql_connect("localhost", "root", "123456"); mysql_select_db("linyimin",$db); // 该用户是否存在 $sql = "select * from admin_info where user_name = \"$user_name\""; $result = mysql_query($sql); $num = mysql_num_rows($result); // 输入名称不存在 if ($num == 0 && $operation != "add") { $user_name = $_POST['user_name']; echo "<script type='text/javascript'>dis_alert(\"$user_name\",2);</script>"; return 0; } else { // 判断有没有权限(只有root权限可以修改) $sql = "select * from admin_info where user_name = \"$NAME\""; $result = mysql_query($sql); $pemission = mysql_result($result,0,'pemission'); // 没有root权限,报错 if(strcmp($pemission,"root") != 0) { $user_name = $_POST['user_name']; echo "<script type='text/javascript'>dis_alert(\"$user_name\",1);</script>"; return 0; } else return 1; } }
Common skills record
1. Use session to realize the method of using the same variable in multiple php filesIn the text that defines the variable Open the session and store the value into the sessionusersession_unset(); session_start(); $_SESSION['变量名'] = "值";Open the session in the text that uses the variable and take out the variable
session_start(); $NAME = $_SESSION['变量名'];2. PHP connects to the MYSQL database and performs search, add, and delete operations on the databaseConnect to the database
// 连接数据库 $db = mysql_connect("url", "用户名", "密码"); // 选择数据库 mysql_select_db("数据库名称",$db);Find
$sql = "select * from admin_info where 字段名 = \"查找值\""; $result = mysql_query($sql); // 对查找返回结果进行操作 // 获取查找返回记录数条数 $num = mysql_num_rows($result); // 获取查找结果第一条记录的user_name字段值 $user_name = mysql_result($result,0,'user_name'); // 逐条取出查询记录 while($row = mysql_fetch_row($result)) { 相关操作; }Insert
$sql = "insert into 数据表 (字段1, 字段2, 字段3) values ('值1','值2','值3')"; mysql_query($sql);Delete
$sql = "delete from 数据表 where 字段名 = \"查找值\""; mysql_query($sql); // 关闭数据库 mysql_close($db);3. Reminder before form submission
The above is the detailed content of Detailed explanation of simple interactive site development with PHP MYSQL. For more information, please follow other related articles on the PHP Chinese website!

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use
