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. JAVA学习笔记16——控制线程

    Java的线程支持提供了一些便捷的工具方法,通过这些便捷的工具方法可以很好地控制线程执行.   join线程 Thread提供了让一个线程等待另一个线程完成的方法——join().当在某个线程执行流中 ...

  2. C# Word 类库

    C# Word 类库 2009-08-06 22:10 14292人阅读 评论(11) 收藏 举报 c#objectstring文档microsoftexcel using System;using ...

  3. Unexpected token d in JSON at position 669 while parsing near '...ct-mode":"^6.0.2"}

    问题 在安装 babel 的时候,遇到问题 Unexpected token d in JSON at position 669 while parsing near '...ct-mode" ...

  4. js页面跳转定位

    A页面 <!DOCTYPE html> <html> <head> <meta name="viewport" content=" ...

  5. 排序算法小结:C++实现

    #include<vector> #include<iostream> //排序算法的稳定性:对于相同的关键字,排序之前的位置和排序之后的位置相同,则称为稳定排序,否则不稳定排 ...

  6. 「 HDU 1978 」 How many ways

    # 解题思路 记忆化搜索 一个点可以跳到的点,取决于它现在的能量.而且有一个显而易见的性质就是一条可行路径的终点和起点的横坐标之差加上纵坐标之差肯定小于等于起点的能量. 因为跳到一个点之后,能量和之前 ...

  7. PermGen space OOM 记录

    前些天线上除出了个OOM问题,今天闲下来记录下: OOM的提示信息是-PermGen space,说明问题出在方法区,方法区存的是什么东西?:类的加载信息.常量.静态变量. 按照方法区的定义:类加载的 ...

  8. buf.fill()

    buf.fill(value[, offset[, end]][, encoding]) value {String} | {Buffer} | {Number} offset {Number} 默认 ...

  9. layuiAdmin 项目修改

    layuiAdmin修改 index.js 修改登录url user/login=>publics/login config.js 修改 name 项目名称, tokenName token字段 ...

  10. LeetCode(55)Jump Game

    题目 Given an array of non-negative integers, you are initially positioned at the first index of the a ...