五、修改数据
在个教程中,我都把要执行的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.