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. vsphere中的linux虚拟机安装vmware-tools

    先在vcenter中选中虚拟机点击安装这个工具,如图 然后这台linux虚拟机的控制台操作,挂载先建立挂载目录 cd /mnt #在挂载建一个用来挂载的文件. mkdir cdrom 使用mount命 ...

  2. 09CSS高级定位

    CSS高级定位 定位方式——position position:static|absolute|relative static表示为静态定位,是默认设置.  absolute表示绝对定位,与下位置属 ...

  3. Jupyter IPython dead kernel and do not restart

    本人遇到的情况:dead kernel & try to restart failed 查看CMD发现这个库安装有问题 解决办法 1.pip uninstall backports.shuti ...

  4. PyTorch: 序列到序列模型(Seq2Seq)实现机器翻译实战

    版权声明:博客文章都是作者辛苦整理的,转载请注明出处,谢谢!http://blog.csdn.net/m0_37306360/article/details/79318644简介在这个项目中,我们将使 ...

  5. JS判断字符串包含的方法

    本文实例讲述了JS判断字符串包含的方法.分享给大家供大家参考.具体如下: 1. 例子: 1 2 3 4 5 6 7 8 var tempStr = "tempText" ; var ...

  6. 服务器端编程心得(二)—— Reactor模式

    最近一直在看游双的<高性能linux服务器编程>一书,下载链接: http://download.csdn.net/detail/analogous_love/9673008 书上是这么介 ...

  7. [Python3网络爬虫开发实战] 3.2.1-基本用法

    1. 准备工作 在开始之前,请确保已经正确安装好了requests库.如果没有安装,可以参考1.2.1节安装. 2. 实例引入 urllib库中的urlopen()方法实际上是以GET方式请求网页,而 ...

  8. odoo 权限配置讲解

    今天来讲解一下odoo权限配置的简单讲解,配合公司开发的权限模块的使用,进行odoo权限配置的说明 BaseSecurityExtend 2.0模块 这是公司自主开发的一款针对odoo菜单级别进行可视 ...

  9. MyBatis 返回Map<String,Object>类型

    <!-- 导出所有数据 --> <select id="exportAll" resultMap="map"> SELECT t1.ME ...

  10. HTML文件中css样式的引用

    1.1.直接在div中使用css样式制作div+css网页 如: <div style="font-size:14px; color:#FF0000;">内容</ ...