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.

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

PHPisusedforsendingemailsduetoitsbuilt-inmail()functionandsupportivelibrarieslikePHPMailerandSwiftMailer.1)Usethemail()functionforbasicemails,butithaslimitations.2)EmployPHPMailerforadvancedfeatureslikeHTMLemailsandattachments.3)Improvedeliverability

PHP performance bottlenecks can be solved through the following steps: 1) Use Xdebug or Blackfire for performance analysis to find out the problem; 2) Optimize database queries and use caches, such as APCu; 3) Use efficient functions such as array_filter to optimize array operations; 4) Configure OPcache for bytecode cache; 5) Optimize the front-end, such as reducing HTTP requests and optimizing pictures; 6) Continuously monitor and optimize performance. Through these methods, the performance of PHP applications can be significantly improved.

DependencyInjection(DI)inPHPisadesignpatternthatmanagesandreducesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itallowspassingdependencieslikedatabaseconnectionstoclassesasparameters,facilitatingeasiertestingandscalability.

CachingimprovesPHPperformancebystoringresultsofcomputationsorqueriesforquickretrieval,reducingserverloadandenhancingresponsetimes.Effectivestrategiesinclude:1)Opcodecaching,whichstorescompiledPHPscriptsinmemorytoskipcompilation;2)DatacachingusingMemc


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

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.

Dreamweaver Mac version
Visual web development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version
Recommended: Win version, supports code prompts!

WebStorm Mac version
Useful JavaScript development tools
