PHP连接数据库(注冊页面的增删改查)
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> <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连接数据库(注冊页面的增删改查)的更多相关文章
- Java简单示例-用户登录、单个页面的增删改查及简单分页
index.html -登录->stulist.jsp (index.html传递到LoginServlet,进行登录检测及写入session,NO返回index.html界面,OK 跳转到s ...
- 项目二:品优购 第二天 AngularJS使用 brand商品页面的增删改查
品优购电商系统开发 第2章 品牌管理 传智播客.黑马程序员 1.前端框架AngularJS入门 1.1 AngularJS简介 AngularJS 诞生于2009年,由Misko Hevery 等人 ...
- go+mysql实现页面的增删改查练习
原文地址:http://www.niu12.com/article/35 初次学go,在了解一些基础之后就开始做一个用户的增删改查来回顾知识,有很多数据验证和安全漏洞并没有考虑,只当作联系 前提:下载 ...
- Python教程:连接数据库,对数据进行增删改查操作
各位志同道合的同仁可以点击上方关注↑↑↑↑↑↑ 本教程致力于程序员快速掌握Python语言编程. 本文章内容是基于上次课程Python教程:操作数据库,MySql的安装详解 和python基础知识之上 ...
- jdbc的连接数据库,使用PreparedStatement实现增删改查等接口
首先是连接,关闭资源等数据库操作 将连接数据库,关闭资源封装在JDBCUtils里 package jdbc.utils; import java.sql.Connection; import jav ...
- 封装MySQL的单例,连接数据库并对数据进行增删改查操作
单例: 一个类只能有一个对象 应用场景:多次请求数据库只需要一个连接对象. 实现:三私一公 1.私有的静态属性用来保存对象的单例2.私有的构造方法用来阻止在类的外部实例化3.私有的__clone阻止在 ...
- 利用PHP连接数据库——实现用户数据的增删改查的整体操作实例
main页面(主页面) <table width="100%" border="1" cellpadding="0" cellspac ...
- iviewUI 前端静态页面实现增删改查分页
完整代码部分 (仅供参考哈): <template> <div> <label prop="name"> 姓名: </label> ...
- solr后台【web页面】增删改查
就是在下面这个页面操作 增加 {"id":"2", "name": "添加"} 查询 id:2 修改 {"id ...
随机推荐
- QStandardItemModel
QString("%1").arg(g_PrjMg.m_Param.stRunParaSet.wWDTTimer) ///站号参数 model = new QStandardIte ...
- c++ include
#include <>与#include " "区别 如果头文件名在<>中,就会被认为是标准头文件.编译器会在预定义的位置查找该头文件,如果是"& ...
- jdk5.0新增两个线程创建方法
1.实现callable接口 import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; ...
- 06CSS列表
CSS列表 列表样式——list-style-type list-style-type:<属性值> disc 黑圆点 circle 空心圆点 square 小黑方块 decimal ...
- JAVA基础——设计模式之观察者模式
观察者模式是对象的行为模式,又叫发布-订阅(Publish/Subscribe)模式.模型-视图(Model/View)模式.源-监听器(Source/Listener)模式或从属者(Dependen ...
- jq进度条
<!doctype html><html><head><meta charset="utf-8"><title>JQue ...
- 树莓派 -- oled
硬件 SPI0,CE0 SPI Master Driver 设备树 arch\arm\boot\dts\bcm2710-rpi-3-b.dts &gpio { spi0_pins: spi0_ ...
- 笔记——python风格规范
分号 不要在行尾加分号, 也不要用分号将两条命令放在同一行. 行长度 每行不超过80个字符 例外: 长的导入模块语句 注释里的URL 不要使用反斜杠连接行. Python会将 圆括号, 中括号和花括号 ...
- xtu Shortest Path
Acceteped : 23 Submit : 61 Time Limit : 5000 MS Memory Limit : 65536 KB Description 题目描述 N(3≤N≤1 ...
- hdu 3622 二分+2-sat
/* 二分+2-sat 题意:在一个二维平面上给你n个炸弹,和2*n个位置,每一行的两个位置只能有一个放炸弹 现在炸弹爆炸有一个半径,当炸弹爆炸时两个炸弹的半径化成的圆不能相交,求最大半径 二分半径, ...