


PHP/MySQL three-day pass - second day (Wednesday)_PHP tutorial
五、修改数据
在个教程中,我都把要执行的SQL语句放到一个变量($sql)中,然后才用mysql_query()来执行数据库查询。在调试时这是很有用的。如果程序出了什么问题,您随时可以把SQL语句的内容显示出来,检查其中的语法错误。
我们已经学习了如何把数据插入到数据库中。现在我们来学习如何修改数据库中已有的记录。数据的编辑包括两部分:数据显示和通过表格输入把数据返回给数据库,这两部分我们前面都已经讲到了。然而,数据编辑还是有一点点不同,我们必须先在表格中显示出相关的数据。
首先,我们回过头再看看第一课的程序代码,在网页中显示员工姓名。但是这次,我们要把数据显示在表格中。程序看起来象下面这样:
$#@60;html$#@62; $#@60;body$#@62; $#@60;?php $db = mysql_connect("localhost", "root"); mysql_select_db("mydb",$db); if ($id) { // 查询数据库 $sql = "SELECT * FROM employees WHERE id=$id"; $result = mysql_query($sql); $myrow = mysql_fetch_array($result); ?$#@62; $#@60;form method="post" action="$#@60;?php echo $PATH_INFO?$#@62;"$#@62; $#@60;input type=hidden name="id" value="$#@60;?php echo $myrow["id"] ?$#@62;"$#@62; 名:$#@60;input type="Text" name="first" value="$#@60;?php echo $myrow["first"] ?$#@62;"$#@62;$#@60;br$#@62; 姓:$#@60;input type="Text" name="last" value="$#@60;?php echo $myrow["last"] ?$#@62;"$#@62;$#@60;br$#@62; 住址:$#@60;input type="Text" name="address" value="$#@60;?php echo $myrow["address"] ?$#@62;"$#@62;$#@60;br$#@62; 职位:$#@60;input type="Text" name="position" value="$#@60;?php echo $myrow["position"] ?$#@62;"$#@62;$#@60;br$#@62; $#@60;input type="Submit" name= bmit" value="输入信息"$#@62; $#@60;/form$#@62; $#@60;?php } else { // 显示员工列表 $result = mysql_query("SELECT * FROM employees",$db); while ($myrow = mysql_fetch_array($result)) { printf("$#@60;a href="%s?id=%s"$#@62;%s %s$#@60;/a$#@62;$#@60;br$#@62; ", $PATH_INFO, $myrow["id"], $myrow["first"], $myrow["last"]); } } ?$#@62; $#@60;/body$#@62; $#@60;/html$#@62; |
We just wrote the field content into the value attribute in the corresponding table element, which is relatively simple. We go one step further and enable the program to write the user-modified content back to the database. Similarly, we use the Submit button to determine whether to process the form input content. Also note that the SQL statements we use are slightly different.
$#@60;html$#@62; $#@60;body$#@62; $#@60;?php $db = mysql_connect("localhost", "root"); mysql_select_db("mydb",$db); if ($id) { if ($submit) { $sql = "UPDATE employees SET first=$first,last=$last, address=$address,position=$position WHERE id=$id"; $result = mysql_query($sql); echo "谢谢!数据更改完成 "; } else { // 查询数据库 $sql = "SELECT * FROM employees WHERE id=$id"; $result = mysql_query($sql); $myrow = mysql_fetch_array($result); ?$#@62; $#@60;form method="post" action="$#@60;?php echo $PATH_INFO?$#@62;"$#@62; $#@60;input type=hidden name="id" value="$#@60;?php echo $myrow["id"] ?$#@62;"$#@62; 名:$#@60;input type="Text" name="first" value="$#@60;?php echo $myrow["first"] ?$#@62;"$#@62;$#@60;br$#@62; 姓:$#@60;input type="Text" name="last" value="$#@60;?php echo $myrow["last"] ?$#@62;"$#@62;$#@60;br$#@62; 住址:$#@60;input type="Text" name="address" value="$#@60;?php echo $myrow["address"] ?$#@62;"$#@62;$#@60;br$#@62; 职位:$#@60;input type="Text" name="position" value="$#@60;?php echo $myrow["position"] ?$#@62;"$#@62;$#@60;br$#@62; $#@60;input type="Submit" name="submit" value="输入信息"$#@62; $#@60;/form$#@62; $#@60;?php } } else { // 显示员工列表 $result = mysql_query("SELECT * FROM employees",$db); while ($myrow = mysql_fetch_array($result)) { printf("$#@60;a href="%s?id=%s"$#@62;%s %s$#@60;/a$#@62;$#@60;br$#@62; ", $PATH_INFO, $myrow["id"], $myrow["first"], $myrow["last"]); } } ?$#@62; $#@60;/body$#@62; $#@60;/html$#@62; |
That’s it. Most of the features we have learned are included in this program. You have also seen that we have added an if() statement to an if() conditional statement to check multiple conditions.
Next, we are going to add everything together and write a good program.

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

AI Hentai Generator
Generate AI Hentai for free.

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



The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

PHP is not dying, but constantly adapting and evolving. 1) PHP has undergone multiple version iterations since 1994 to adapt to new technology trends. 2) It is currently widely used in e-commerce, content management systems and other fields. 3) PHP8 introduces JIT compiler and other functions to improve performance and modernization. 4) Use OPcache and follow PSR-12 standards to optimize performance and code quality.

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

How to connect to MySQL using phpMyAdmin? The URL to access phpMyAdmin is usually http://localhost/phpmyadmin or http://[your server IP address]/phpmyadmin. Enter your MySQL username and password. Select the database you want to connect to. Click the "Connection" button to establish a connection.
