在PHP设计数据库中如何增删改查源代码(下)
3.更新
<html>
<body>
<title>
更新
</title>
<?
//PHP程序的开始
$db = MySQL_connect("localhost", "root");
MySQL_select_db("mysite",$db);
if (!$id)
{
$result = MySQL_query("SELECT * FROM user",$db);
if ($myrow = MySQL_fetch_array($result))
{
printf("<h2 align=left>请选择要修改的用户名字:</h2>");
do {
printf("%s<a href="%s?id=%s">%s </a><br>n",
$myrow["id"],$PATH_INFO,$myrow["id"], $myrow["name"]);
}
while ($myrow = MySQL_fetch_array($result));
}
else
{
echo "对不起!数据库中没有记录";
}
}
else
{
if ($submit)
{
$sql = "UPDATE user SET name='$name',city='$city',
address='$address',email='$email' WHERE id=$id";
$result = MySQL_query($sql);
echo "谢谢!数据更改完成n";
}
else
{
$sql = "SELECT * FROM user WHERE id=$id";
$result = MySQL_query($sql);
$myrow = MySQL_fetch_array($result);
//以下不是PHP的部分
?>
<h2 align=left>请对用户的信息进行修改:</h2>
<form method="post" action="<? echo $PATH_INFO ?>">
<input type=hidden name="id"
value="<?php echo $myrow["id"] ?>">
姓名:<input type="Text" name="first" value="<?php
echo $myrow["name"] ?>"><br>
城市:<input type="Text" name="city" value="<?php echo
$myrow["city"] ?>"><br>
住址:<input type="Text" name="address" value="<?php echo
$myrow["address"] ?>"><br>
电子邮件:<input type="Text" name="email" value="<?php echo
$myrow["email"] ?>"><br>
<input type="Submit" name="submit" value="修改完毕">
</form>
<?
//PHP 的结束
65:}
}
?>
</body>
</html>
4.增加删除记录
<html>
<body>
<title>
增加删除记录
</title>
<?
$db = MySQL_connect("localhost", "root");
MySQL_select_db("mysite",$db);
if ($submit)
{
$sql = "INSERT INTO myuser (name,city,address,email)
VALUES ('$name','$city','$address','$email')";
$result = MySQL_query($sql);
echo "记录添加成功!<p>";
}
else
if ($delete)
{
// 删除一条记录
$sql = "DELETE FROM myuser WHERE id=$id";
$result = MySQL_query($sql);
echo "记录删除成功!<p>";
}
else
{
printf("<h2 align=left>删除一个用户的所有信息:</h2>");
$result = MySQL_query("SELECT * FROM myuser",$db);
while ($myrow = MySQL_fetch_array($result))
{
printf("%s:<a href="%s?id=%s&delete=yes">%s</a> <br>n",
$myrow["id"],$PATH_INFO, $myrow["id"], $myrow["name"]);
}
?>
<P>
<h2 align=left>
添加一个新的用户:
</h2>
<form method="post" action="<?php echo $PATH_INFO?>">
姓名:<input type="Text" name="name" value="<?php
echo $myrow["name"] ?>"><br>
城市:<input type="Text" name="city" value="<?php echo
$myrow["city"] ?>"><br>
住址:<input type="Text" name="address" value="<?php echo
$myrow["address"] ?>"><br>
电子邮件:<input type="Text" name="email" value="<?php echo
$myrow["email"] ?>"><br>
<input type="Submit" name="submit" value="添加用户">
</form>
<?php
}
?>
</body>
</html>
www.zzzyk.com 每天都为您更新最新的电脑知识信息