数据库mydb
表格info,nation,login
效果
<!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> <span>用户名:</span><input type="text" name="uid" /></div>
<div> <span>密 码:</span><input type="text" name="pwd" /></div>
<div><input type="submit" name="btn" value="登录" /></div>
</form>
</body>
</html>
<!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="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"]."%'"; } //else {$strsel = " where Code = '".$_POST["code"]."'";}可以省略 } else { if(@$_POST["name"]!= null) { $strsel = " where Name like '%".$_POST["name"]."%'"; } //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>
<!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 class mydbda { var $host="localhost"; var $username="root"; var $password="123"; var $database="mydb"; /* 功能:执行SQL语句,返回结果 参数:$sql:要执行的SQL语句 $type:SQL语句的类型,CX代表查询,QT代表其他 $data:要操作的数据库 返回值:如果是查询,返回结果集 如果是其他语句,执行成功返回ok,失败返回no */ function select($sql,$type,$data) { $db=new mysqli($this->host,$this->username,$this->password,$data); if(mysqli_connect_error()) { echo "连接失败"; exit; } else { $result=$db->query($sql); if($type=="CX") { //return $result; 用拼接字符串替换掉 $str=""; while($row=$result->fetch_row()) { for($i=0;$i<count($row);$i++) { $str=$str.$row[$i]."^"; } $str=substr($str,0,strlen($str)-1);//去掉"|"前面的"^" $str = $str."|"; //n001^汉族^|n002^回族^|n003^苗族^| } $str=substr($str,0,strlen($str)-1);//去掉"|" return $str; } else { if($result) { return "ok"; } else { return "no"; } } } } /*function denglu($uid,$pwd) { $db=new mysqli($this->host,$this->username,$this->password,$this->database); if(mysqli_connect_error()) { echo "连接失败"; exit; } else { $sql="select * from login where UserName='{$uid}' and Password='{$pwd}'" ; $result=$db->query($sql); if($row=$result->fetch_row()) { return "ok"; } else { return "no"; } } }*/ } ?> </body> </html>
<!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="0127tjchuli.php" method="post"> <div><span>代号:</span><input type="text" name="code" /></div> <div><span>姓名:</span><input type="text" name="name" /></div> <div><span>性别:</span><input type="radio" checked="checked" name="sex" value="true"/>男 <input type="radio" name="sex" value="false"/>女 </div> <div> <span>民族:</span> <select name="nation"> <?php $db=new mysqli("localhost","root","123","mydb"); if(mysqli_connect_error()) { echo "连接错误"; } else { $sql="select * from nation"; $result=$db->query($sql); while($row=$result->fetch_row()) { echo "<option value='{$row[0]}'>{$row[1]}</option>"; } } ?> </select> </div> <div><span>生日:</span><input type="text" name="birthday"/></div> <div><input type="submit" value="添加" /> <a href="0127lianxi.php">返回</a></div> </form> </body> </html>
<!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=$_GET["code"]; $db=new mysqli("localhost","root","123","mydb"); if(mysqli_connect_error()) { echo "连接错误"; } else { $sql="delete from Info Where code='{$code}'"; $result=$db->query($sql); if($result) { header("Location:0127lianxi.php"); } else { echo "删除失败"; } } ?> </body> </html>
<!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> <?php $code=$_GET["code"]; $db=new mysqli("localhost","root","123","mydb"); if(mysqli_connect_error()) { echo "连接错误"; } else { $sql="select * from Info where code='".$code."'"; $result=$db->query($sql); $row=$result->fetch_row(); } ?> <form action="0127xgchuli.php" method="post"> <div><span>代号:</span><input type="text" name="code" value="<?php echo $row[0] ?>" readonly="readonly"/></div> <div><span>姓名:</span><input type="text" name="name" value="<?php echo $row[1] ?>" /></div> <div><span>性别:</span><input type="radio" <?php echo (bool)$row[2]?"checked='checked'":"" ?> name="sex" value="true"/>男 <input type="radio" name="sex" value="false" <?php echo !(bool)$row[2]?"checked='checked'":"" ?>/>女 </div> <div> <span>民族:</span> <select name="nation"> <?php $db=new mysqli("localhost","root","123","mydb"); if(mysqli_connect_error()) { echo "连接错误"; } else { $sql="select * from nation"; $result=$db->query($sql); while($rownation=$result->fetch_row()) { if($rownation[0]==$row[3]) { echo "<option selected='selected' value='{$rownation[0]}' >{$rownation[1]}</option>"; } else { echo "<option value='{$rownation[0]}'>{$rownation[1]}</option>"; } } } ?> </select> </div> <div><span>生日:</span><input type="text" name="birthday" value="<?php echo $row[4] ?>"/></div> <div><input type="submit" value="修改" /> <a href="0127lianxi.php">返回</a></div> </form> </body> </html>
<!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>
<!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"]; $db=new mysqli("localhost","root","123","mydb"); if(mysqli_connect_error()) { echo "连接错误"; } else { $sql="insert into Info values('{$code}','{$name}',{$sex},'{$nation}','{$birthday}')"; $result=$db->query($sql); if($result) { header("Location:0127tianjia.php"); } else { echo "添加失败"; } } ?> </body> </html>
0127tjchuli.php中的$sex写法要注意bit和varchar的变量写法