永州网,内容丰富有趣,生活中的好帮手!
永州网 > 头条 > 正文

PHP/MySQL三日通:深入学习第二天

时间:2024-02-03

本书旨在帮助初学者快速掌握PHP和MySQL的使用技巧,涵盖了从基础到进阶的知识点

友情提示:本文共有 5017 个字,阅读大概需要 11 分钟。

"PHP/MySQL三日通-第二天(三)"是一本深入探讨PHP和MySQL的教程的第二天内容的简要介绍。本书旨在帮助初学者快速掌握PHP和MySQL的使用技巧,涵盖了从基础到进阶的知识点。第二天的内容将重点介绍MySQL数据库的设计与优化、SQL语句的高级应用、PHP与MySQL的连接与操作等内容。通过系统化的学习方式和多种实例演练,读者将能够更加深入地理解PHP与MySQL的使用,为日后的应用和开发奠定坚实的基础。

给自己一点时间接受自己,爱自己,趁着下午茶的时间来学习推荐的PHP/MySQL三日通-第二天(三),过去的都会过去,迎接崭新的开始,释放更美好的自己。

五、修改数据

 

在个教程中,我都把要执行的SQL语句放到一个变量($sql)中,然后才用mysql_query()来执行数据库查询。在调试时这是很有用的。如果程序出了什么问题,您随时可以把SQL语句的内容显示出来,检查其中的语法错误。

我们已经学习了如何把数据插入到数据库中。现在我们来学习如何修改数据库中已有的记录。数据的编辑包括两部分:数据显示和通过表格输入把数据返回给数据库,这两部分我们前面都已经讲到了。然而,数据编辑还是有一点点不同,我们必须先在表格中显示出相关的数据。

首先,我们回过头再看看第一课的程序代码,在网页中显示员工姓名。但是这次,我们要把数据显示在表格中。程序看起来象下面这样:

[email protected];[email protected];

[email protected];[email protected];

[email protected];?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);

?[email protected];

[email protected];form method="post" action="[email protected];?php echo $PATH_INFO?[email protected];"[email protected];

[email protected];input type=hidden name="id" value="[email protected];?php echo $myrow["id"] ?[email protected];"[email protected];

名:[email protected];input type="Text" name="first" value="[email protected];?php echo

$myrow["first"] ?[email protected];"[email protected];[email protected];[email protected];

姓:[email protected];input type="Text" name="last" value="[email protected];?php echo

$myrow["last"] ?[email protected];"[email protected];[email protected];[email protected];

住址:[email protected];input type="Text" name="address" value="[email protected];?php echo

$myrow["address"] ?[email protected];"[email protected];[email protected];[email protected];

职位:[email protected];input type="Text" name="position" value="[email protected];?php echo

$myrow["position"] ?[email protected];"[email protected];[email protected];[email protected];

[email protected];input type="Submit" name= bmit" value="输入信息"[email protected];

[email protected];[email protected];

[email protected];?php

} else {

// 显示员工列表

$result = mysql_query("SELECT* FROM employees",$db);

while ($myrow = mysql_fetch_array($result)) {

printf("[email protected];a href="%s?id=%s"[email protected];%s[email protected];[email protected];[email protected];[email protected];n", $PATH_INFO,

$myrow["id"], $myrow["first"], $myrow["last"]);

}

}

?[email protected];

[email protected];[email protected];

[email protected];[email protected];

[email protected]�的value属性里,这是相应简单的。我们再往前进一步,使程序可以把用户修改过的内容写回数据库去。同样,我们通过Submit按钮来判断是否处理表格输入内容。还要注意,我们用的SQL语句稍稍有些不同。

[email protected];[email protected];

[email protected];[email protected];

[email protected];?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 "谢谢!数据更改完成n";

} else {

// 查询数据库

$sql = "SELECT * FROM employees WHERE id=$id";

$result = mysql_query($sql);

$myrow = mysql_fetch_array($result);

?[email protected];

[email protected];form method="post" action="[email protected];?php echo $PATH_INFO?[email protected];"[email protected];

[email protected];input type=hidden name="id" value="[email protected];?php echo $myrow["id"] ?[email protected];"[email protected];

名:[email protected];input type="Text" name="first" value="[email protected];?php

echo $myrow["first"] ?[email protected];"[email protected];[email protected];[email protected];

姓:[email protected];input type="Text" name="last" value="[email protected];?php echo

$myrow["last"] ?[email protected];"[email protected];[email protected];[email protected];

住址:[email protected];input type="Text" name="address" value="[email protected];?php echo

$myrow["address"] ?[email protected];"[email protected];[email protected];[email protected];

职位:[email protected];input type="Text" name="position" value="[email protected];?php echo

$myrow["position"] ?[email protected];"[email protected];[email protected];[email protected];

[email protected];input type="Submit" name="submit" value="输入信息"[email protected];

[email protected];[email protected];

[email protected];?php

}

} else {

// 显示员工列表

$result = mysql_query("SELECT * FROM employees",$db);

while ($myrow = mysql_fetch_array($result)) {

printf("[email protected];a href="%s?id=%s"[email protected];%s %s$#@60;/a$#@62;$#@60;br$#@62;n", $PATH_INFO,

$myrow["id"], $myrow["first"], $myrow["last"]);

}

}

?$#@62;

$#@60;/body$#@62;

$#@60;/html$#@62;

就是这样。在这个程序中已经包含了我们学过所大多数特性。您也已经看到,我们在一个if()条件判别语句中又加了一个if()语句,来检查多重条件。

下面,我们要把所有东西全都加在一起,写出一个很好的程序来。

收集不易,本文《PHP/MySQL三日通:深入学习第二天》知识如果对你有帮助,请点赞收藏并留下你的评论。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。
显示评论内容(4)
  1. 龙香2024-02-03 11:02龙香[澳门网友]203.22.31.242
    第二天就深入学习了,学习效率真高啊!
    顶8踩0
  2. 红杏待出墙2024-02-03 10:54红杏待出墙[火星网友]115.69.76.59
    学习PHP/MySQL真是太有趣了,加油!
    顶4踩0
  3. 窗前枕月2024-02-03 10:47窗前枕月[国外网友]115.120.183.16
    @遗风期待看到最终成果,加油!
    顶10踩0
  4. 遗风2024-02-03 10:40遗风[台湾省网友]114.111.21.172
    真是太棒了,学习速度这么快!
    顶11踩0
相关阅读
实现网站多模板切换的方法解析

实现网站多模板切换的方法解析

其中,雷柏V500PRO多模版就是我比较喜欢的机械键盘,它提供了蓝牙5.0、蓝牙3.0、无线2.4G和有线共4种连接模式

2024-02-01 #推荐

如何用标段造句:打造清晰流畅的语言表达

如何用标段造句:打造清晰流畅的语言表达

2、文章主要介绍了沿江十标段预制箱梁的施工方法及质量控制经验

2024-01-09 #随笔

广州男子将女友针头藏共享单车座垫中,引发警方通报

广州男子将女友针头藏共享单车座垫中,引发警方通报

广州市公安局白云区分局 12 月 6 日通报称,2023 年 12 月 4 日 16 时许,广州白云警方接到某共享单车平台工作人员报警,称收到

2023-12-06 #头条

甘肃临夏大河家黄河大桥现裂缝,大型车辆遭禁通

甘肃临夏大河家黄河大桥现裂缝,大型车辆遭禁通

受地震影响,G310甘肃临夏市东乡段大河家黄河大桥K1964+151出现裂缝,只允许应急车辆及小型车辆通行,大型车辆禁止通行

2023-12-19 #推荐

清华北大相继宣布:恢复正常教学秩序

清华北大相继宣布:恢复正常教学秩序

↓ ↓ ↓清华大学暑期校园参观管理公告在保障校园正常教学、科研和生活秩序的前提下,清华大学秉承坚持开放、加强服务、有序管理的原则,对社会公众开放暑期校园参观,具体事项...

2024-01-24 #百科

公司回应强制加班到晚上10点请假算旷工:系对新员工考察

公司回应强制加班到晚上10点请假算旷工:系对新员工考察

吴先生晚上7时请假下班,被人事要求签自愿离职申请书

2023-11-21 #头条

“广西老表”爆火 黄牛3倍加价

“广西老表”爆火 黄牛3倍加价

在20世纪90年代库存手表卖一块少一块的情绪烘托下,南宁当地出现了黄牛加价和打卡风潮,导致厂家临时出台限购措施、呼吁理性消费

2023-11-24 #推荐

精选合同协议书模板集合:九篇必备范本

精选合同协议书模板集合:九篇必备范本

那么相关的协议书到底怎么写呢?下面是小编为大家整理的合同协议书9篇,欢迎大家分享

2024-01-14 #推荐