1.连接数据库

—————————–connect.php——————————————–

<?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页面)

———————————-reg_9.php—————————————

<!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>


3.将注冊数据显示在数据库

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

———————————-hello.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<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>&nbsp;&nbsp;&nbsp;&nbsp;<a href='delete.php? id=$value[id]'>删除</a></td>";
echo "<tr/>";
echo "</table>";
}
?>


4.改动数据

—————————————–update1.php———————————–

//当用户要改动信息时,返回页面,页面中包括之前填写的信息

<!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>

————————————–update.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('改动失败')";
}
?>

5.删除数据

—————————–delete.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('删除失败')";
}
?>

//若要删除李四,点击删除后。会自己主动跳转到后台页面,数据库里数据也删除

PHP连接数据库(注冊页面的增删改查)的更多相关文章

  1. Java简单示例-用户登录、单个页面的增删改查及简单分页

    index.html  -登录->stulist.jsp (index.html传递到LoginServlet,进行登录检测及写入session,NO返回index.html界面,OK 跳转到s ...

  2. 项目二:品优购 第二天 AngularJS使用 brand商品页面的增删改查

    品优购电商系统开发 第2章 品牌管理 传智播客.黑马程序员 1.前端框架AngularJS入门 1.1 AngularJS简介 AngularJS  诞生于2009年,由Misko Hevery 等人 ...

  3. go+mysql实现页面的增删改查练习

    原文地址:http://www.niu12.com/article/35 初次学go,在了解一些基础之后就开始做一个用户的增删改查来回顾知识,有很多数据验证和安全漏洞并没有考虑,只当作联系 前提:下载 ...

  4. Python教程:连接数据库,对数据进行增删改查操作

    各位志同道合的同仁可以点击上方关注↑↑↑↑↑↑ 本教程致力于程序员快速掌握Python语言编程. 本文章内容是基于上次课程Python教程:操作数据库,MySql的安装详解 和python基础知识之上 ...

  5. jdbc的连接数据库,使用PreparedStatement实现增删改查等接口

    首先是连接,关闭资源等数据库操作 将连接数据库,关闭资源封装在JDBCUtils里 package jdbc.utils; import java.sql.Connection; import jav ...

  6. 封装MySQL的单例,连接数据库并对数据进行增删改查操作

    单例: 一个类只能有一个对象 应用场景:多次请求数据库只需要一个连接对象. 实现:三私一公 1.私有的静态属性用来保存对象的单例2.私有的构造方法用来阻止在类的外部实例化3.私有的__clone阻止在 ...

  7. 利用PHP连接数据库——实现用户数据的增删改查的整体操作实例

    main页面(主页面) <table width="100%" border="1" cellpadding="0" cellspac ...

  8. iviewUI 前端静态页面实现增删改查分页

    完整代码部分 (仅供参考哈): <template> <div> <label prop="name"> 姓名: </label> ...

  9. solr后台【web页面】增删改查

    就是在下面这个页面操作 增加 {"id":"2", "name": "添加"} 查询 id:2 修改 {"id ...

随机推荐

  1. 手动配置webpack

    //注:“__dirname”是node.js中的一个全局变量,它指向当前执行脚本所在的目录.const path = require('path');const webpack = require( ...

  2. NPOI--------------.Net操作Excel初步使用(导出)

    背景 因公司项目需要添加数据导出功能故此添加,找了几种方式发现该方式具有 无需依赖本机安装office环境,使用灵活等优点故采用此方式. 安装 Nuget 直接安装NPOI即可 使用方式 1.根据需要 ...

  3. TX-LCN事务控制原理

    TX-LCN由两大模块组成, TxClient.TxManager,TxClient作为模块的依赖框架,提供TX-LCN的标准支持,TxManager作为分布式事务的控制方.事务发起方或者参与方都由T ...

  4. android ViewPager实现的轮播图广告自定义视图,网络获取图片和数据

    public class SlideShowAdView extends FrameLayout { //轮播图图片数量    private static int IMAGE_COUNT = 3;  ...

  5. JS授权

    (function(){ var origin_url = location.href; var oauth_url = 'https://vx.mcilife.com/weixin/api/oaut ...

  6. CSU2188: Substring

    Description FST 是一名可怜的 ACMer,他很强,但是经常 fst,所以 rating 一直低迷. 但是重点在于,他真的很强!他发明了一种奇特的加密方式,这种加密方式只有 ACMer ...

  7. docker配置国内加速器

    一.登录到daocloud网站后选择如下地址的加速器 二.根据配置提示在linux上执行对应的配置命令: curl -sSL https://get.daocloud.io/daotools/set_ ...

  8. python Django 相关学习笔记

    Django框架 pip3 install django 命令: # 创建Django程序 django-admin startproject mysite # 进入程序目录 cd mysite # ...

  9. bzoj 3173 [Tjoi2013]最长上升子序列 (treap模拟+lis)

    [Tjoi2013]最长上升子序列 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2213  Solved: 1119[Submit][Status] ...

  10. Django:(5)分页器 & forms组件

    Django组件:分页器 目录结构: urls.py from django.contrib import admin from django.urls import path from app01 ...