您的位置:首页 > 编程语言 > PHP开发

PHP——修改数据库2-加提示框,加登录页面

2016-01-27 23:39 676 查看
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<?php
$code=$_POST["code"];
$name=$_POST["name"];
$sex=$_POST["sex"];
$nation=$_POST["nation"];
$birthday=$_POST["birthday"];
//1.造连接对象
$db=new mysqli("localhost","root","123","mydb");
//2.判断是否连接成功
if(mysqli_connect_error())
{
echo "连接失败";
}
else
{
//3.写语句
$sql="update info set name='".$name."',sex='".$sex."',nation='".$nation."',birthday='".$birthday."'where code='".$code."'";
//4.执行sql语句
$result = $db->query($sql);
//判断是否修改成功
if($result)
{
header("Location:0127lianxi.php");
}
else
{
echo "修改失败!";
}

}

?>
</body>
</html>


0127xgchuli.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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<h1>主页面</h1>
<form action="0127lianxi.php" method="post">
<div style="width:100%; height:40px">
<span>代号:</span>
<input type="text"  name="code"/>   
<span>姓名:</span>
<input type="text"  name="name"/>  
<input type="submit" value="查询" name="btn"/>
</div>
</form>

<div>
<?php

//登录代码
/*include("0127mydbda.php");

$uid=$_POST["uid"];
$pwd=$_POST["pwd"];

$dl = new mydbda();

$sqldl = "select * from login where UserName='{$uid}' and Password='{$pwd}'";
$jieguo = $dl->select($sqldl,"CX","mydb");
if($jieguo=="")
{
header("Location:0127denglu.php");
}
else
{
}*/

/*if($rowdl=$jieguo->fetch_row())//与后面的return $result对应
{
}
else
{
header("Location:0127denglu.php");
}*/

/*if($dl->denglu($uid,$pwd)=="ok")//通过上面方法优化
{
}
else
{
header("Location:0127denglu.php");
}*/

//查询代码

$strsel = "";

if(@$_POST["code"] != null)
{
$strsel = " where Code = '".$_POST["code"]."'";  //where前面必须要有空格

if(@$_POST["name"]!= null)
{
$strsel = " where Code='".$_POST["code"]."' and Name like '%".$_POST["name"]."%'";//where前面必须有空格
}
//else {$strsel = " where Code = '".$_POST["code"]."'";}可以省略
}
else
{
if(@$_POST["name"]!= null)
{
$strsel = " where Name like '%".$_POST["name"]."%'";//where前面必须有空格
}
//else{$strsel="";}都为空执行最开始的$strsel
}

//1.连接数据可以
$db = new mysqli("localhost","root","123","mydb");
//2.判断是否连接成功
if(mysqli_connect_error())
{
echo "连接失败";
}
else
{
//3.写sql语句
$sql = "select * from Info".$strsel;
//4.执行sql语句
$result=$db->query($sql);
//5.处理数据,遍历数据

echo "<table width=90% cellpadding=0 cellspacing=0 border=1>";
echo "<tr> <td>代号</td> <td>姓名</td> <td>性别</td> <td>民族</td> <td>生日</td> <td>操作</td> </tr>";
while($row=$result->fetch_row())
{
//改性别
$sex=$row[2]?"男":"女";
//改民族
$nation=NationName($db,$row[3]);
//改生日
$birthday=date("Y年m月d日",strtotime($row[4]));

echo "<tr bgcolor='#00FFCC'> <td>{$row[0]}</td> <td>{$row[1]}</td> <td>{$sex}</td> <td>{$nation}</td> <td>{$birthday}</td> <td><a href='0127sc.php?code=".$row[0]."' onclick=\"return confirm('确定删除吗?')\">删除</a>  <a href='0127xiugai.php?code=".$row[0]."'>修改</a></td> </tr>";//\" \"双引号里出现双引号转义字符用

}
echo "</table>";
}

function NationName($db,$code)
{
//写sql语句
$sql="select * from nation where code='{$code}'";
//4.执行sql语句
$result=$db->query($sql);
//5.处理数据
if($row=$result->fetch_row())
{
return $row[1];
}
else
{
return "";
}

}

?>
</div>
<div><a href="0127tianjia.php">添加数据</a></div>
<form>
<input type="submit" value="提交" onclick="return confirm('确定么')" />
</form>
</body>
</html>


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: