本文实例为大家分享了PHP连接数据库实现注册页面的增删改查操作的方法,供大家参考,具体内容如下
1.连接数据库
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页面)
xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
http-equiv="Content-Type" content="text/html;charset=UTF-8" />
Document
注册页面
action="add.php" method='post'>
border='1' cellpadding='0' cellspacing='0' width='80%' bgcolor='#ABCDEF'>
align='right'>用户名
type="text" name="username" id=""/>以小写字母开始,长度要求5~10
align='right'>密码
type="password" name="password" id=""/>密码不能为空
align='right'>邮箱
type="text" name="email" id="" />
align='right'>性别
type="radio" name="sex" id="" value='1' />男
type="radio" name="sex" id="" value='2' />女
type="radio" name="sex" id="" value='3' />保密
align='right'>个人简介
name="txt" id="" cols="50" rows="10">
colspan='2'> type="submit" name='act' value='注册' />
3.将注册数据显示在数据库
//往数据库中添加数据
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.返回后台界面
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".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 "";
echo "";
echo " ";
echo "".$value['id']."";
echo "".$value['username']."";
echo "".$value['password']."";
echo "".$value['email']."";
echo "".$value['sex']."";
echo "".$value['txt']."";
echo "".date('Y-m-d H:i:s',$value['time'])."";
echo "修改删除";
echo "";
echo "";
}
?>
5.修改数据
//当用户要修改信息时,返回页面,页面中包含之前填写的信息
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">
郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。
新闻热点
疑难解答