PHP的数据库增删改查源代码
PHP的数据库增删改查源代码
//mysql数据库的sql
CREATE TABLE user(id int(5)DEFAULT‘0‘NOT NULL AUTO_INCREMENT,name varchar(20),city varchar(20),postcode varchar(10),address varchar(100),password varchar(10),level tinyint(2)DEFAULT‘0‘,right tinyint(2),email varchar(40),PRIMARY KEY(id),UNIQUE id(id));
INSERT INTO myuser VALUES(1,‘王友‘,‘北京‘,‘100083‘,‘北京市海淀区学院路‘,111,0,0,‘wangyou000@263.net‘);
INSERT INTO myuser VALUES(2,‘金风‘,‘武汉‘,‘430071‘,‘武汉市武昌区水果湖‘,111,0,0,‘jinfeng000@263.net‘);
INSERT INTO myuser VALUES(3,‘常丽‘,‘襄樊‘,‘441021‘,‘襄樊市襄城区南街‘,111,0,0,‘changli000@263.net‘);
1.查寻全部
下面是用户数据库的所有内容:
$db=MySQL_connect("localhost","root");
MySQL_select_db("mysite",$db);
$result=MySQL_query("SELECT*FROM user",$db);
echo"n";
echo"id姓名";
echo"城市邮编";
echo"住址等级";
echo"权限邮件地址";
echo"n";
while($myrow=MySQL_fetch_row($result))
{
printf("%s%s",$myrow[0],$myrow);
printf("%s%s",$myrow,$myrow);
printf("%s%s",$myrow,$myrow);
printf("%s%sn",$myrow,$myrow);
}
echo"n";
?>
2.根据id查询
$db=MySQL_connect("localhost","root");
MySQL_select_db("mysite",$db);
if($id){
$result=MySQL_query("SELECT*FROM user WHERE ,$db);
$myrow=MySQL_fetch_array($result);
printf("
用户的情况");
printf("1.ID号%sn
",$myrow["id"]);
printf("2.姓名:%sn
",$myrow["name"]);
printf("3.城市:%sn
",$myrow["city"]);
printf("4.邮编:%sn
",$myrow["postcode"]);
printf("5.住址:%sn
",$myrow["address"]);
printf("6.等级:%sn
",$myrow["level"]);
printf("7.邮件地址:%sn
",$myrow["email"]);
printf("",$PATH_INFO);
}
else
{
$result=MySQL_query("SELECT*FROM user",$db);
if($myrow=MySQL_fetch_array($result))
{
printf("
请选择要查询的用户:");
do{
printf("%s
n",
$myrow["id"],$PATH_INFO,$myrow["id"],$myrow["name"]);
}
while($myrow=MySQL_fetch_array($result));
}
else
{
echo"对不起,没有数据!";
}
}
?>
3.更新
//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("
请选择要修改的用户名字:");
do{
printf("%s
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 ;
$result=MySQL_query($sql);
echo"谢谢!数据更改完成n";
}
else
{
$sql="SELECT*FROM user WHERE ;
$result=MySQL_query($sql);
$myrow=MySQL_fetch_array($result);
//以下不是PHP的部分
?>
请对用户的信息进行修改:
//PHP的结束
65:}
}
?>
4.增加删除记录
$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"记录添加成功!
";
}
else
if($delete)
{
//删除一条记录
$sql="DELETE FROM myuser WHERE ;
$result=MySQL_query($sql);
echo"记录删除成功!
";
}
else
{
printf("
删除一个用户的所有信息:");
$result=MySQL_query("SELECT*FROM myuser",$db);
while($myrow=MySQL_fetch_array($result))
{
printf("%s:
n",
$myrow["id"],$PATH_INFO,$myrow["id"],$myrow["name"]);
}
?>
添加一个新的用户:
}
?>