本文实例为大家分享了php连接数据库实现注册页面的增删改查操作的方法,供大家参考,具体内容如下
1.连接数据库
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?php
//本地测试
$host = '127.0.0.1' ;
$port = 3306;
$user = "root" ;
$pwd = "" ;
$link = @mysql_connect( "{$host}:{$port}" , $user , $pwd ,true);
if (! $link ) {
die ( "connect server failed: " . mysql_error());
}
//选择连接的数据库库名
mysql_select_db( "my" );
//设置字符编码utf8
mysql_set_charset( 'utf8' );
?>
|
2.注册页面(html页面)
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd" >
<html xmlns= "http://www.w3.org/1999/xhtml" xml:lang= "en" >
<head>
<meta http-equiv= "content-type" content= "text/html;charset=utf-8" />
<title>document</title>
</head>
<body>
<h3>注册页面</h3>
<form action= "add.php" method= 'post' >
<table border= '1' cellpadding= '0' cellspacing= '0' width= '80%' bgcolor= '#abcdef' >
<tr>
<td align= 'right' >用户名</td>
<td><input type= "text" name= "username" id= "" />以小写字母开始,长度要求5~10</td>
</tr>
<tr>
<td align= 'right' >密码</td>
<td><input type= "password" name= "password" id= "" />密码不能为空</td>
</tr>
<tr>
<td align= 'right' >邮箱</td>
<td><input type= "text" name= "email" id= "" /></td>
</tr>
<tr>
<td align= 'right' >性别</td>
<td>
<input type= "radio" name= "sex" id= "" value= '1' />男
<input type= "radio" name= "sex" id= "" value= '2' />女
<input type= "radio" name= "sex" id= "" value= '3' />保密
</td>
</tr>
<tr>
<td align= 'right' >个人简介</td>
<td>
<textarea name= "txt" id= "" cols= "50" rows= "10" ></textarea>
</td>
</tr>
<tr>
<td colspan= '2' ><input type= "submit" name= 'act' value= '注册' /></td>
</tr>
</table>
</form>
</body>
</html>
|
?
1
2
3
4
5
6
7
8
9
10
11
12
|
//往数据库中添加数据
<?php
header( "content-type:text/html; charset=utf-8" );
//-----------------------连接数据库---------------------------
include_once "connect.php" ;
//-------------------------将数据连接到数据库------------------
$time =time();
$sql = "insert into user (username,password,email,sex,txt,`time`) value('{$_post['username']}','{$_post['password']}','{$_post['email']}','{$_post['sex']}','{$_post['txt']}','{$time}')" ;
$res =mysql_query( $sql );
header( "location:hello.php" );
?>
|
4.返回后台界面
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
<?php
header( "content-type:text/html; charset=utf-8" );
//-----------------------连接数据库------------------------------
include_once "connect.php" ;
//--------------------查询数据库--------------------------------
$query = "select * from user" ;
$result =mysql_query( $query );
if (! $result )
{
die ( "could not to the database<br/>" .mysql_error());
}
//-------------------封装函数-----------------------------
//该函数将数据库的数据写成数组形式
function result2arr( $result ){
while ( $result_row =mysql_fetch_assoc( $result )){
$arr [] = $result_row ;
}
return $arr ;
}
$arr = result2arr( $result );
foreach ( $arr as $key => $value ){
echo "<table border='1px'>" ;
echo "<table border='1px' >" ;
echo "<tr> " ;
echo "<td width='100px'>" . $value [ 'id' ]. "</td>" ;
echo "<td width='100px'>" . $value [ 'username' ]. "</td>" ;
echo "<td width='100px'>" . $value [ 'password' ]. "</td>" ;
echo "<td width='200px'>" . $value [ 'email' ]. "</td>" ;
echo "<td width='100px'>" . $value [ 'sex' ]. "</td>" ;
echo "<td width='100px'>" . $value [ 'txt' ]. "</td>" ;
echo "<td width='100px'>" . date ( 'y-m-d h:i:s' , $value [ 'time' ]). "</td>" ;
echo "<td width='100px'><a href='update1.php?id=$value[id]'>修改</a><a href='delete.php?id=$value[id]'>删除</a></td>" ;
echo "<tr/>" ;
echo "</table>" ;
}
?>
|
5.修改数据
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
//当用户要修改信息时,返回页面,页面中包含之前填写的信息
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd" >
<html xmlns= "http://www.w3.org/1999/xhtml" xml:lang= "en" >
<head>
<meta http-equiv= "content-type" content= "text/html;charset=utf-8" />
<title>document</title>
</head>
<body>
<div>
<?php
include_once "connect.php" ;
$sql = "select * from user where id='" . $_get ['id ']."' ";
//echo "sql:".$sql;(显示出修改哪一行)
$result =mysql_query( $sql , $link );
$arr = result2arr( $result );
//print_r($arr);
$row = $arr [0];
function result2arr( $result ){
while ( $result_row =mysql_fetch_assoc( $result )){
$arr [] = $result_row ;
}
return $arr ;
}
?>
<h3>注册页面</h3>
<form action= "update.php" method= 'post' >
<input type= "hidden" name= "id" id= "" value= "<?php echo $row['id']?>" />
<table border= '1' cellpadding= '0' cellspacing= '0' width= '80%' bgcolor= '#abcdef' >
<tr>
<td align= 'right' >用户名</td>
<td><input type= "text" name= "username" id= "" value= "<?php echo $row['username']?>" />以小写字母开始,长度要求5~10</td>
</tr>
<tr>
<td align= 'right' >密码</td>
<td><input type= "password" name= "password" id= "" value= "<?php echo $row['password']?>" />密码不能为空</td>
</tr>
<tr>
<td align= 'right' >邮箱</td>
<td><input type= "text" name= "email" id= "" value= "<?php echo $row['email']?>" /></td>
</tr>
<tr>
<td align= 'right' >性别</td>
<td>
<input type= "radio" name= "sex" id= "" value= '1' <?php if ( $row [ 'sex' ]== '1' ){ echo 'checked' ;}?>/>男
<input type= "radio" name= "sex" id= "" value= '2' <?php if ( $row [ 'sex' ]== '2' ){ echo 'checked' ;}?>/>女
<input type= "radio" name= "sex" id= "" value= '3' <?php if ( $row [ 'sex' ]== '3' ){ echo 'checked' ;}?>/>保密
</td>
</tr>
<tr>
<td align= 'right' >个人简介</td>
<td>
<textarea name= "txt" id= "" cols= "50" rows= "10" ><?php echo $row [ 'txt' ]?></textarea>
</td>
</tr>
<tr>
<td colspan= '2' ><input type= "submit" name= 'act' value= '修改' /></td>
</tr>
</table>
</form>
</div>
</body>
</html>
|
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
//将修改的信息存入数据库
<?php
header( "content-type:text/html; charset=utf-8" );
//通过post获取页面提交数据信息
$data = $_post ;
//print_r($data);
include_once "connect.php" ;
$sql = "update `user` set username='{$data['username']}',password='{$data['password']}', email='{$data['email']}',sex='{$data['sex']}',txt='{$data['txt']}' where id='{$data['id']}'" ;
echo $sql ;
$res = mysql_query( $sql , $link );
if ( $res ){
header( "location:hello.php" );
//echo "alert('修改成功')";
} else {
header( "location:update1.php?id=" . $data [ 'id' ]);
//echo "alert('修改失败')";
}
?>
|
6.删除数据
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
//删除数据库里的数据
<?php
header( "content-type:text/html; charset=utf-8" );
include_once 'connect.php' ;
$sql = "delete from user where id='" . $_get ['id ']."' ";
$sus =mysql_query( $sql , $link );
if ( $sus ){
header( "location:hello.php" );
} else {
echo "alert('删除失败')" ;
}
?>
//若要删除李四,点击删除后,会自动跳转到后台页面,数据库里数据也删除
|
以上就是本文的全部内容,希望对大家的学习有所帮助。
相关文章
猜你喜欢
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-06-04 34
-
2025-05-25 23
-
2025-05-25 54
-
2025-05-29 61
-
2025-05-27 34
热门评论