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

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

1.连接数据库

rush:PHP;">

2.注册页面(html页面

rush:xhtml;"> Document

注册页面

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

注册数据显示数据库

rush:PHP;"> //往数据库添加数据

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

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

5.修改数据

rush:xhtml;"> //当用户修改信息时,返回页面页面中包含之前填写的信息

<!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;
}
?>

注册页面

用户名密码邮箱性别dio" name="sex" id="" value='1' />男 dio" name="sex" id="" value='2' />女 dio" name="sex" id="" value='3' />保密 个人简介

rush:PHP;"> //将修改的信息存入数据库

<?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.删除数据

rush:PHP;"> //删除数据库里的数据

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

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

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...