


Example of adding, deleting, modifying and checking user data by connecting to the database in PHP
The following editor will bring you an overall operation example of using PHP to connect to the database to implement addition, deletion, modification and query of user data. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.
main page(main page)
<table width="100%" border="1" cellpadding="0" cellspacing="0"> <tr> <td>代号</td> <td>姓名</td> <td>性别</td> <td>民族</td> <td>生日</td> <td>操作</td> </tr> <?php $db = new MySQLi("localhost","root","","mydb"); if(mysqli_connect_error()){ die("连接失败"); } $sql = "select * from Info"; $result = $db->query($sql); $attr = $result->fetch_all(); foreach($attr as $v) { $sex = $v[2]? '男':'女'; //三元运算符判断性别 $sql = "select Name from Nation where Code ='$v[3]'"; $result = $db ->query($sql); $attr = $result->fetch_assoc(); echo "<tr> <td>{$v[0]}</td> <td>{$v[1]}</td> <td>{$sex}</td> <td>{$attr['Name']}</td> <td>{$v[4]}</td> <td> <a href='Delete.php?code={$v[0]}'>删除</a> <a href='Update.php?code={$v[0]}'>修改</a> </td> </tr>"; } ?> </table> <p> <a href="Add.php" rel="external nofollow" >添加数据</a> </p>
Add(Add data page)
##
<h1>添加数据</h1> <form action="AddChuLi.php" method="post"> <p>代号:<input type="text" name="code" /></p> <p>姓名:<input type="text" name="name" /></p> <p>性别: <input type="radio" value="男" name="sex" />男 <input type="radio" value="女" name="sex" />女 </p> <p>民族: <select name="nation"> <?php $db = new MySQLi("localhost","root","","mydb"); if(mysqli_connect_error()){ die("连接失败"); } $sql = "select * from Nation"; $r = $db->query($sql); $att = $r->fetch_all(); foreach($att as $v) { echo "<option value='{$v[0]}'>{$v[1]}</option>"; } ?> </select> </p> <p>生日:<input type="text" name="birthday" /></p> <p><input type="submit" value="添加数据" /></p> </form>
AddChuLi page(Add data processing page)
<?php $code = $_POST["code"]; $name = $_POST["name"]; $sex = $_POST["sex"]; $s = 1; if($sex =="女") { $s=0; } $nation = $_POST["nation"]; $birthday = $_POST["birthday"]; $db = new MySQLi("localhost","root","","mydb"); if(mysqli_connect_error()){ die("连接失败"); } $sql = "insert into Info values('{$code}','{$name}','{$s}','{$nation}','{$birthday}')"; //添加数据语句 $result = $db->query($sql); if($result) { header("location:main.php"); //php跳转页面方式 } else { echo "添加失败!"; }
DeleteChuLi page (Delete data processing page)
<?php $code = $_GET["code"]; $db = new MySQLi("localhost","root","","mydb"); if(mysqli_connect_error()){ die("连接失败"); } $sql = "delete from Info where Code ='{$code}'"; //删除语句 $r = $db->query($sql); if($r) { header("location:main.php"); } else { echo "删除失败!"; }
Update page (modify data page)
<h1>修改数据</h1> <?php $code = $_GET["code"]; $db = new MySQLi("localhost","root","","mydb"); if(mysqli_connect_error()){ die("连接失败"); } $sql1 = "select * from Info where Code='{$code}'"; $r1 = $db->query($sql1); $att1 = $r1->fetch_row(); ?> <form action="UpdateChuLi.php" method="post"> <p>代号:<input type="hidden" name="code" value="<?php echo $att1[0] ?>" /></p> <p>姓名:<input type="text" name="name" value="<?php echo $att1[1] ?>" /></p> <p>性别: <input type="radio" value="男" name="sex" <?php echo $att1[2] ? "checked='checked'" : ""; ?> />男 <input type="radio" value="女" name="sex" <?php echo $att1[2] ? "" : "checked='checked'"; ?> />女 </p> <p>民族: <select name="nation"> <?php $db = new MySQLi("localhost","root","","mydb"); if(mysqli_connect_error()){ die("连接失败"); } $sql = "select * from Nation"; //查询nation一组数据 $r = $db->query($sql); $att = $r->fetch_all(); foreach($att as $v) { if($att1[3]==$v[0]) { echo "<option value='{$v[0]}' selected='selectec' >{$v[1]}</option>"; } else { echo "<option value='{$v[0]}'>{$v[1]}</option>"; } } ?> </select> </p> <p>生日:<input type="text" name="birthday" value="<?php echo $att1[4] ?>"/></p> <p><input type="submit" value="修改数据" /></p> </form>
ateChuLi page (modify data Processing page)
<?php $code = $_POST["code"]; $name = $_POST["name"]; $sex = $_POST["sex"]; $s=1; if($sex=="女") { $s=0; } $nation = $_POST["nation"]; $birthday = $_POST["birthday"]; $db = new MySQLi("localhost","root","","mydb"); if(mysqli_connect_error()){ die("连接失败"); } $sql = "update Info set Name='{$name}',Sex={$s},Nation='{$nation}',Birthday='{$birthday}' where Code='{$code}'"; //修改数据语句 $r = $db->query($sql); if($r) { header("location:main.php"); } else { echo "修改失败!"; }
The above is the detailed content of Example of adding, deleting, modifying and checking user data by connecting to the database in PHP. For more information, please follow other related articles on the PHP Chinese website!

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
