登录
<form action="chuli.php" method="post">
<div>用户名:<input type="text" name="uid" /></div>
<div>密码:<input type="password" name="pwd" /></div>
<div><input type="submit" value="登录" /></div>
</form>
登录处理
<?php
$uid = $_POST["uid"];
$pwd = $_POST["pwd"];
//造连接对象
$db = new MySQLi("localhost","root","123","mydb");
//写SQL语句
//SQL注入攻击
$sql = "select password from login where username='{$uid}'";
//执行SQL语句
$reslut = $db->query($sql);
$n = $reslut->fetch_row();
if($uid!="" && $pwd !="" )
{
if($n[0]==$pwd)
{
header("location:main.php");
}
else
{
echo "用户名或密码错误!";
}
}
else
{
echo "用户名密码不能为空";
}
数据显示
<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","mydb");
//写SQL语句
$sql = "select * from info";
//执行SQL语句
$result = $db->query($sql);
//读数据
$attr = $result->fetch_all();
foreach($attr as $v)
{
echo "<tr>";
$sex = $v[2]?"男":"女";
//根据名族代号查询名族名称
$name = NationName($v[3]);
echo "<td>{$v[0]}</td><td>{$v[1]}</td><td>{$sex}</td><td>{$name}</td><td>{$v[4]}</td><td><a href='shanchu.php?c={$v[0]}' onclick=\"return confirm('确定删除么?')\">删除</a></td>";
/*foreach($v as $v1)
{
echo "<td>{$v1}</td>";
}*/
echo "</tr>";
}
//给一个民族代号,返回民族名称
function NationName($code)
{
//造连接对象
$db = new MySQLi("localhost","root","123","mydb");
//写SQL语句
$sql = "select name from nation where code='{$code}'";
//执行SQL语句
$result = $db->query($sql);
$attr = $result->fetch_row();
return $attr[0];
}
?>
</table>
<a href="add.php"><input type="button" value="添加数据" /></a>
</body>
删除
<?php
$code = $_GET["c"];
//造连接对象
$db = new MySQLi("localhost","root","123","mydb");
$sql = "delete from info where code='{$code}'";
$r = $db->query($sql);
if($r)
{
header("location:main.php");
}
else
{
echo "删除失败";
}
修改
<form action="addchuli.php" method="post">
<div>代号:<input type="text" name="code" /></div>
<div>姓名:<input type="text" name="name" /></div>
<div>性别:
<input type="radio" name="sex" value="1" checked="checked" />男
<input type="radio" name="sex" value="0" />女
</div>
<div>民族:
<select name="nation">
<?php
//造连接对象
$db = new MySQLi("localhost","root","123","mydb");
$sql = "select * from nation";
$result = $db->query($sql);
$attr = $result->fetch_all();
foreach($attr as $v)
{
echo "<option value='{$v[0]}'>{$v[1]}</option>";
}
?>
</select>
</div>
<div>生日:<input type="text" name="birthday" /></div>
<div><input type="submit" value="添加" /></div>
</form>
</body>
修改处理
<?php
$code = $_POST["code"];
$name = $_POST["name"];
$sex = $_POST["sex"];
$nation = $_POST["nation"];
$birthday = $_POST["birthday"];
//造连接对象
$db = new MySQLi("localhost","root","123","mydb");
$sql = "insert into info values('{$code}','{$name}',{$sex},'{$nation}','{$birthday}')";
$db->query($sql);
header("location:main.php");