加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP连接数据库实现注册页面的增删改查操作

发布时间:2020-12-12 21:25:18 所属栏目:PHP教程 来源:网络整理
导读:本文实例为大家分享了PHP连接数据库实现注册页面的增删改查操作的方法,供大家参考,具体内容如下 1.连接数据库 2.注册页面(html页面) Document 注册页面 用户名 密码 邮箱 性别 男 女 保密 个人简介 ?php header("Content-type:text/html; charset=utf-8"

本文实例为大家分享了PHP连接数据库实现注册页面的增删改查操作的方法,供大家参考,具体内容如下

1.连接数据库

2.注册页面(html页面)

Document

注册页面

用户名密码邮箱性别男 保密 个人简介

<?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.返回后台界面

".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 "".$value['id']."".$value['username']."".$value['password']."".$value['email']."".$value['sex']."".$value['txt']."".date('Y-m-d H:i:s',$value['time'])."修改删除"; echo "

5.修改数据

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

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

以上就是本文的全部内容,希望对大家的学习有所帮助。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!