本文实例为大家分享了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页面)
Document 注册页面

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 "
| ".$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 "

5.修改数据
//当用户要修改信息时,返回页面,页面中包含之前填写的信息Document <?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; } ?>注册页面

//将修改的信息存入数据库
<?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.删除数据
//删除数据库里的数据
<?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('删除失败')";
}
?>
//若要删除李四,点击删除后,会自动跳转到后台页面,数据库里数据也删除

以上就是本文的全部内容,希望对大家的学习有所帮助。
相关推荐
- php判断当前页面是不是在微信里面打开
- PHP 采集心得技巧
- DISCUZ 论坛管理员密码忘记的解决方法
- PHP获取当前文件所在目录 getcwd()函数
- 示例详解Laravel的注册重构
- PHP实现路由映射到指定控制器
- 谈谈PHP连接Access数据库的注意事项
- 总结PHP中DateTime的常用方法
- php使用curl详细解析及问题汇总
- 示例详解Laravel重置密码代码重构
- 修改Laravel5.3中的路由文件与路径
- 教你在header中隐藏php的版本信息
- PHP怎样用正则抓取页面中的网址
- php阳历转农历优化版
- 常用PHP数组排序函数归纳
- PHP+JQuery+Ajax实现分页方法详解
- 微信自定义菜单的创建/查询/取消php示例代码
- Thinkphp微信公众号支付接口
- 浅析Laravel5中队列的配置及使用
- php微信支付接口开发程序


