2016/3/26 连接数据库 网页中数据的增删改 add delete update addchuli updateChuLi test8 DBDA
2024-08-30 16:55:33
主页面 test8.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<table width=100% border="1" cellpadding="0" cellspacing="0">
<tr>
<td>代号</td>
<td>姓名</td>
<td>性别</td>
<td>民族</td>
<td>生日</td>
<td>操作</td>
</tr>
<?php
//造连接对象
$db=new mysqli("localhost","root","123","test2");
//判断连接是否成功
!mysqli_connect_error()or die("连接失败!");
//写sql语句
$sql="select * from Info";
//执行sql语句
$result=$db->query($sql);
//处理查询的结果
$attr=$result->fetch_all();
for ($i=0; $i <count($attr) ; $i++) {
echo "<tr>";
for ($j=0; $j <count($attr[$i]);$j++)
{
echo "<td>{$attr[$i][$j]}</td>";
}
//GET提交方式手写出来拼接出来
//'delete.php?code={$attr[$i][0]}'
echo "<td><a href='delete.php?code={$attr[$i][0]}'>删除</a><a href='update.php?code={$attr[$i][0]}'>修改</a></td>";
echo "</tr>";
} ?>
</table>
<br/>
<a href="Add.php"><input type="button" value="添加数据"/></a>
</body>
</html>
数据库封装成类 页面 DBDA.php
<?php
$code=$_POST["code"];
$name=$_POST["name"];
$sex=$_POST["sex"];
$nation=$_POST["nation"];
$birthday=$_POST["birthday"]; //造连接对象
$db=new mysqli("localhost","root","123","test2");
//判断是否出错
!mysqli_connect_error() or die("连接失败!");
//写sql语句
$sql="update info set name='$name',sex= $sex,nation='$nation',birthday='$birthday'where code='$code'";//sex= $sex 返回布尔型值 不能加引 否则无法把女改男
//执行语句
$result=$db->query($sql);
if ($result) {
header("location:test8.php");
}
else
{
echo"修改失败!";
}
?>}
Add.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="Addchuli.php" method="post">
<div>
代号:
<input type="text" name="code"/>
<!-- 提交必有name -->
</div>
<div>
姓名:
<input type="text" name="name"/>
</div>
<div>
性别:
男<input type="radio" value="true" name="sex" checked="checked" />
女<input type="radio" value="false"name="sex"/>
</div>
<div>
民族:
<select name="nation" >
<?php
//造连接对象
$db=new mysqli("localhost","root","123","test2");
//判断连接是否成功
!mysqli_connect_error()or die("连接失败!");
//写sql语句
$sql="select * from Nation";
//出现提示fetch_row/all错误 找sql语句可能表错符号空格多少的问题
//执行sql语句
$result=$db->query($sql);
//处理查询的结果
$attr=$result->fetch_all();
for ($i=0; $i <count($attr) ; $i++) { echo "<option value='{$attr[$i][0]}'>{$attr[$i][1]}</option>";
}
?>
</select>
</div>
<div>
生日:
<input type="text" name="birthday"/>
</div>
<div>
<input type="submit" value="确定"/>
<a href="test8.php">返回主页</a>
</div>
</form>
</body>
</html>
Addchuli.php
<?php
$code=$_POST["code"];
$name=$_POST["name"];
$sex=$_POST["sex"];
$nation=$_POST["nation"];
$birthday=$_POST["birthday"]; //造连接对象
$db=new mysqli("localhost","root","123","test2");
//判断是否出错
!mysqli_connect_error() or die("连接失败!");
//写sql语句
$sql="insert into Info values('$code','$name',$sex,'$nation','$birthday')";
//执行语句
$result=$db->query($sql);
if ($result) {
header("location:Add.php");
}
else{
echo"执行失败!";
} ?>
Update.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<?php
$code=$_GET["code"]; $db=new mysqli("localhost","root","123","test2");
//判断连接是否成功
!mysqli_connect_error()or die("连接失败!");
//写sql语句
$sqlx="select * from Info where Code='$code'";
//执行sql语句
$result=$db->query($sqlx);
$attx=$result->fetch_row();
?>
<form action="UpdateChuLi.php" method="post">
<div>
代号:
<input readonly="readonly" type="text" name="code" value="<?php echo $attx[0]; ?>"/>
</div>
<div>
姓名:
<input type="text" name="name" value="<?php echo $attx[1]; ?>";/>
</div>
<div>
性别:
男<input type="radio" value="true" name="sex" <?php echo $attx[2]?"checked='checked'":""; ?> />
女<input type="radio" value="false" name="sex" <?php echo $attx[2]?"":"checked='checked'"; ?>/>
</div>
<div>
民族:
<select name="nation" >
<?php
// 上面有了连接对象,下面这个可以删掉前两句
//造连接对象
//$db=new mysqli("localhost","root","123","test2");
//判断连接是否成功
//!mysqli_connect_error()or die("连接失败!");
//写sql语句
$sql="select * from nation";
//执行sql语句
$result=$db->query($sql);
//处理查询的结果
$attr=$result->fetch_all();
for ($i=0; $i <count($attr) ; $i++) {
if ($attx[3]==$attr[$i][0])
{
echo"<option selected='selected' value='{$attr[$i][0]}'>{$attr[$i][1]}</option>";
}
else
{
echo "<option value='{$attr[$i][0]}'>{$attr[$i][1]}</option>";
}
}
?>
</select>
</div>
<div>
生日:
<input type="text" name="birthday" value="<?php echo $attx[4]; ?>" />
</div>
<div>
<input type="submit" value="确定"/>
<a href="test8.php">返回主页</a>
</div>
</form>
</body>
</html>
UpadateChuLi.php
<?php
$code=$_POST["code"];
$name=$_POST["name"];
$sex=$_POST["sex"];
$nation=$_POST["nation"];
$birthday=$_POST["birthday"]; //造连接对象
$db=new mysqli("localhost","root","123","test2");
//判断是否出错
!mysqli_connect_error() or die("连接失败!");
//写sql语句
$sql="update info set name='$name',sex= $sex,nation='$nation',birthday='$birthday'where code='$code'";//sex= $sex 返回布尔型值 不能加引 否则无法把女改男
//执行语句
$result=$db->query($sql);
if ($result) {
header("location:test8.php");
}
else
{
echo"修改失败!";
}
?>}
delete.php
<?php
$code=$_GET["code"];
$db=new mysqli("localhost","root","123","test2");
//判断连接是否成功
!mysqli_connect_error()or die("连接失败!");
//写sql语句
$sql="delete from Info where code='$code'";
//执行sql语句
$result=$db->query($sql);
if ($result) {
header("location:test8.php");
}
else
{
echo "删除失败!";
}
?>
显示效果:
主页表:
增加:
删除:
修改:
最新文章
- statusbarhidden stuff 状态栏的各种特性
- Qt:QObject translate
- Could not load type from string value 'DALMSSQL.DBSessionFactory,DALMSSQL'.
- PostgreSQL基础整理(三)
- 如何解决Response.Redirect方法传递汉字丢失或乱码问题?
- Oracle创建表格报ORA-00906:缺失左括号错误解决办法
- thinkphp中文验证码不能显示的问题
- sql删除多余重复的数据只保留一条
- C#联调C++项目
- VS2013中常用的一些快捷键
- Android内存控制小技巧-使用矢量图来节省你的内存并简化你的开发。
- android 删除文件以及递归删除文件夹
- poj2503--Babelfish(特里一水)
- Mac打开Terminal报错-bash : : command not found
- org.apache.commons.lang.exception.NestableRuntimeException
- 对ThreadLocal实现原理的一点思考
- 生成banner的网站
- Svn在eclipse中使用
- 【洛谷】【线段树】P1047 校门外的树
- toast components
热门文章
- 条款33:避免遮掩继承而来的名称(Avoiding hiding inherited names)
- Photoshop保存的各种格式详解
- ASP.NET Core on K8S学习初探(1)K8S单节点环境搭建
- bzoj 1251序列终结者 splay 区间翻转,最值,区间更新
- struts2 标签库使用
- 前端接收到的json的属性的首字母会自动变成小写,解决办法如下
- springboot注释详解
- angularjs ngRoute的使用简单例子
- Educational Codeforces Round 37 (Rated for Div. 2) G
- git获取远程分支