Home Backend Development PHP Tutorial Detailed explanation of simple interactive site development with PHP MYSQL

Detailed explanation of simple interactive site development with PHP MYSQL

May 29, 2018 am 10:14 AM
mysql php interactive

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[&#39;SCRIPT_NAME&#39;] ?>">
 用户名: <input type="text" name="user_name">
 密码: <input type="text" name="password">
 <input type="Submit" name="submit" value="登陆">
 </form>
</center>
Copy after login

The effect is as follows:

Detailed explanation of simple interactive site development with PHP MYSQL

2 .Connect to the database to verify the login name and password

//登陆处理
 if (isset($_POST[&#39;submit&#39;])) 
 {
  // 用户名输入为空
  if($_POST[&#39;user_name&#39;] == &#39;&#39;)
  // 调用javascript函数动态提醒
  echo "<script type=&#39;text/javascript&#39;>dis_alert(\"用户名\",1);</script>";// 密码输入为空
  if($_POST[&#39;password&#39;] == &#39;&#39;)
  // 调用javascript函数动态提醒
  echo "<script type=&#39;text/javascript&#39;>dis_alert(\"密码\",1);</script>";
  // 用户名与密码均不为空
  $user_name = $_POST[&#39;user_name&#39;];
  //链接数据库,从中读出用户名和密码
  $db = mysql_connect("localhost", "root", "123456");
  mysql_select_db("linyimin", $db);
  $result = mysql_query("select * from login where user_name = &#39;$user_name&#39;");
  $num = mysql_num_rows($result);
  //判断用户输入的用户名存在,验证密码
  if($num != 0)
  {
  $user_name = mysql_result($result,0,&#39;user_name&#39;);
  $password = mysql_result($result,0,&#39;password&#39;);
  if(strcmp($password,$_POST[&#39;password&#39;]) != 0)
  {
   echo "密码错误";
   //密码错误,报错
   $password = $_POST[&#39;password&#39;];
   echo "<script type=&#39;text/javascript&#39;>dis_alert(&#39;密码错误&#39;,3);</script>";
  }
  // 密码正确
  else
  {
   session_unset();
   session_start();
   $_SESSION[&#39;user_name&#39;] = $_POST[user_name];
   header("Location:http://localhost/display.php");
   exit;
  }
  }
  // 用户输入的用户名不存在,报错 
  else if($num == 0)
  {
  // 用户名不存在,报错
  $user_name = $_POST[&#39;user_name&#39;];
  echo "<script type=&#39;text/javascript&#39;>dis_alert(\"$user_name\",2);</script>";
  }
  mysql_close($db); 
 }//登陆处理结束
Copy after login

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>
Copy after login

Error reminder rendering:

Detailed explanation of simple interactive site development with PHP MYSQL

3. After successful login, all user information in the database and the current login user name will be displayed

// 获取登陆名
session_start();
$NAME = $_SESSION[&#39;user_name&#39;];
// 连接数据库,获取数据并显示
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);
}
Copy after login

The display effect is as follows:

Detailed explanation of simple interactive site development with PHP MYSQL

4. Implementation of modification, deletion and addition operations

Modify and add page

<center>
 <form method="post" action="<?php echo $_SERVER[&#39;URL&#39;] ?>">
 姓名: <input type="text" name="user_name">
 权限: <input type="text" name="pemission">
 职务: <input type="text" name="position">
 <input type="Submit" name="update" value="提交">
 </form>
</center>
Copy after login

The effect is as follows:

Detailed explanation of simple interactive site development with PHP MYSQL

Delete page

 <center> 
 <form method="post" action="<?php echo $_SERVER[&#39;URL&#39;] ?>" onsubmit="return confirm(&#39;请确认删除&#39;);">
 姓名: <input type="text" name="user_name">
 <input type="submit" name="update" value="删除">
</center>
Copy after login

The rendering is as follows:

Detailed explanation of simple interactive site development with PHP MYSQL

##Realization

// 调用修改函数
if ($_GET[update]) 
{
 modify("update");
}
// 调用添加函数
elseif($_GET[add])
{
 modify("add");
}
elseif($_GET[delete])
{
 modify("delete");
}
Copy after login

Implementation of modify() function

// 修改数据函数
/*
点击修改超链接,跳转到修改页面
表单中,名字项指定要修改记录
权限和职务项为可修改内容
*/
function modify($operation)
{
 if(isset($_POST[&#39;update&#39;]))
 {
 // 有root权限修改,修改
 if($operation == "update" && judge("update"))
 {
  $user_name = $_POST[user_name];
  $sql = "UPDATE admin_info SET pemission = &#39;$_POST[pemission]&#39;, position =&#39;$_POST[position]&#39; WHERE user_name = &#39;$user_name&#39;";
  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 (&#39;$_POST[user_name]&#39;,&#39;$_POST[pemission]&#39;,&#39;$_POST[position]&#39;)";
  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);
  }
 }
}
Copy after login

Implementation of judge() function

// 判断修改用户名是否存在和该用户是否具有权限
function judge($operation)
{
 global $NAME;
 // 修改用户名
 $user_name = $_POST[&#39;user_name&#39;];
 // 连接数据库,获取数据
 $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[&#39;user_name&#39;];
  echo "<script type=&#39;text/javascript&#39;>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,&#39;pemission&#39;);
  // 没有root权限,报错
  if(strcmp($pemission,"root") != 0)
  {
  $user_name = $_POST[&#39;user_name&#39;];
  echo "<script type=&#39;text/javascript&#39;>dis_alert(\"$user_name\",1);</script>";
  return 0;
  }
  else 
  return 1;
 } 
}
Copy after login

Common skills record

1. Use session to realize the method of using the same variable in multiple php files

In the text that defines the variable Open the session and store the value into the session

usersession_unset();
session_start();
$_SESSION[&#39;变量名&#39;] = "值";
Copy after login

Open the session in the text that uses the variable and take out the variable

session_start();
$NAME = $_SESSION[&#39;变量名&#39;];
Copy after login

2. PHP connects to the MYSQL database and performs search, add, and delete operations on the database

Connect to the database

// 连接数据库
$db = mysql_connect("url", "用户名", "密码");
// 选择数据库
mysql_select_db("数据库名称",$db);
Copy after login

Find

$sql = "select * from admin_info where 字段名 = \"查找值\"";
$result = mysql_query($sql);
// 对查找返回结果进行操作
// 获取查找返回记录数条数
$num = mysql_num_rows($result);
// 获取查找结果第一条记录的user_name字段值
$user_name = mysql_result($result,0,&#39;user_name&#39;);
// 逐条取出查询记录
while($row = mysql_fetch_row($result))
{
 相关操作;
}
Copy after login

Insert

$sql = "insert into 数据表 (字段1, 字段2, 字段3) values (&#39;值1&#39;,&#39;值2&#39;,&#39;值3&#39;)";
mysql_query($sql);
Copy after login

Delete

$sql = "delete from 数据表 where 字段名 = \"查找值\"";
mysql_query($sql);
// 关闭数据库
mysql_close($db);
Copy after login

3. Reminder before form submission


4. Call the javascript function in php

<?php
echo "<script type=&#39;text/javascript&#39;>javascript函数;</script>";
?>
Copy after login

The above is this article The entire content, I hope it will be helpful to everyone's study.


Related recommendations:

php framework CodeIgniterDatabaseDetailed configuration steps

PHP mysql implements the method of obtaining the drop-down tree function from database

PHP implements the use of mysqli to operate MySQLdatabase Methods

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1668
14
PHP Tutorial
1273
29
C# Tutorial
1255
24
MySQL: The Database, phpMyAdmin: The Management Interface MySQL: The Database, phpMyAdmin: The Management Interface Apr 29, 2025 am 12:44 AM

MySQL and phpMyAdmin can be effectively managed through the following steps: 1. Create and delete database: Just click in phpMyAdmin to complete. 2. Manage tables: You can create tables, modify structures, and add indexes. 3. Data operation: Supports inserting, updating, deleting data and executing SQL queries. 4. Import and export data: Supports SQL, CSV, XML and other formats. 5. Optimization and monitoring: Use the OPTIMIZETABLE command to optimize tables and use query analyzers and monitoring tools to solve performance problems.

Composer: Aiding PHP Development Through AI Composer: Aiding PHP Development Through AI Apr 29, 2025 am 12:27 AM

AI can help optimize the use of Composer. Specific methods include: 1. Dependency management optimization: AI analyzes dependencies, recommends the best version combination, and reduces conflicts. 2. Automated code generation: AI generates composer.json files that conform to best practices. 3. Improve code quality: AI detects potential problems, provides optimization suggestions, and improves code quality. These methods are implemented through machine learning and natural language processing technologies to help developers improve efficiency and code quality.

What is the significance of the session_start() function? What is the significance of the session_start() function? May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

Steps to add and delete fields to MySQL tables Steps to add and delete fields to MySQL tables Apr 29, 2025 pm 04:15 PM

In MySQL, add fields using ALTERTABLEtable_nameADDCOLUMNnew_columnVARCHAR(255)AFTERexisting_column, delete fields using ALTERTABLEtable_nameDROPCOLUMNcolumn_to_drop. When adding fields, you need to specify a location to optimize query performance and data structure; before deleting fields, you need to confirm that the operation is irreversible; modifying table structure using online DDL, backup data, test environment, and low-load time periods is performance optimization and best practice.

How to uninstall MySQL and clean residual files How to uninstall MySQL and clean residual files Apr 29, 2025 pm 04:03 PM

To safely and thoroughly uninstall MySQL and clean all residual files, follow the following steps: 1. Stop MySQL service; 2. Uninstall MySQL packages; 3. Clean configuration files and data directories; 4. Verify that the uninstallation is thorough.

An efficient way to batch insert data in MySQL An efficient way to batch insert data in MySQL Apr 29, 2025 pm 04:18 PM

Efficient methods for batch inserting data in MySQL include: 1. Using INSERTINTO...VALUES syntax, 2. Using LOADDATAINFILE command, 3. Using transaction processing, 4. Adjust batch size, 5. Disable indexing, 6. Using INSERTIGNORE or INSERT...ONDUPLICATEKEYUPDATE, these methods can significantly improve database operation efficiency.

How to use MySQL functions for data processing and calculation How to use MySQL functions for data processing and calculation Apr 29, 2025 pm 04:21 PM

MySQL functions can be used for data processing and calculation. 1. Basic usage includes string processing, date calculation and mathematical operations. 2. Advanced usage involves combining multiple functions to implement complex operations. 3. Performance optimization requires avoiding the use of functions in the WHERE clause and using GROUPBY and temporary tables.

Detailed explanation of the installation steps of MySQL on macOS system Detailed explanation of the installation steps of MySQL on macOS system Apr 29, 2025 pm 03:36 PM

Installing MySQL on macOS can be achieved through the following steps: 1. Install Homebrew, using the command /bin/bash-c"$(curl-fsSLhttps://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)". 2. Update Homebrew and use brewupdate. 3. Install MySQL and use brewinstallmysql. 4. Start MySQL service and use brewservicesstartmysql. After installation, you can use mysql-u

See all articles