Home Backend Development PHP Tutorial PHP/MySQL three-day pass - second day (Wednesday)_PHP tutorial

PHP/MySQL three-day pass - second day (Wednesday)_PHP tutorial

Jul 13, 2016 pm 05:20 PM
mysql php sql three indivual Revise exist I put Tutorial data want statement Pass

       五、修改数据

  在个教程中,我都把要执行的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;
Copy after login

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.

This news contains 2 pages, currently on page 1 1 2

  $#@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.

This news contains 2 pages, currently on page 2 1 2

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/532612.htmlTechArticle5. Modify data In this tutorial, I put the SQL statement to be executed into a variable ($sql ), and then use mysql_query() to execute the database query. This is useful when debugging...
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 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)

The Future of PHP: Adaptations and Innovations The Future of PHP: Adaptations and Innovations Apr 11, 2025 am 12:01 AM

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.

How to open phpmyadmin How to open phpmyadmin Apr 10, 2025 pm 10:51 PM

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 vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

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.

How to use single threaded redis How to use single threaded redis Apr 10, 2025 pm 07:12 PM

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 It Dying or Simply Adapting? PHP: Is It Dying or Simply Adapting? Apr 11, 2025 am 12:13 AM

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: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

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.

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

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.

phpmyadmin connection mysql phpmyadmin connection mysql Apr 10, 2025 pm 10:57 PM

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.

See all articles