访问数据库实现增删改查以及页面显示的变化稍微动态性的应用
添加数据:
主页面:Main.php
<?php
$db = new MySQLi("localhost","root","","mydb");
!mysqli_connect_error() or die("连接失败");
$sql = "select * from Info";
$result = $db->query($sql);
$attr = $result->fetch_all();
if($result)
{
foreach($attr as $v)
{
//处理性别
$sex = $v[2]?'男':'女';
//处理民族
$sql1 = "select name from Nation where Code ='{$v[3]}'";
$rnation = $db->query($sql1);
$attr1 = $rnation->fetch_assoc();
echo "<tr>
<td>{$v[0]}</td>
<td>{$v[1]}</td>
<td>{$sex}</td>
<td>{$attr1['name']}</td>
<td>{$v[4]}</td>
<td>
<a href=''>删除</a>
<a href=''>修改</a>
</td>
</tr>";
}
}
?>
</table>
<div><a href="add.php">添加数据</a></div>
添加页面:add.php
<h1>添加数据</h1>
<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" />男
<input type="radio" name="sex" />女
</div>
<div>民族:<select name="nation">
<?php
$db =new MySQLi("localhost","root","","mydb");
!mysqli_connect_error() or die("连接失败");
$sql ="select * from nation";
$result = $db->query($sql);
if($result)
{
$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>
<div><a href="Main.php">主页面</a></div>
添加处理页面:addchuli.php
<?php
$code = $_POST["code"];
$name = $_POST["name"];
$sex = $_POST["sex"];
//处理性别
$s = 1;
if($sex=="女")
{
$s = 0;
}
$nation = $_POST["nation"];
$birthday = $_POST["birthday"];
$db = new MySQLi("localhost","root","","mydb");
$sql = "insert into Info values('{$code}','{$name}',{$s},'{$nation}','{$birthday}')";
$result = $db->query($sql);
if($result)
{
header("location:add.php");//跳转到Add.php
}
else
{
echo "添加失败";
}
以上是添加的过程,分为三个页面需要同时操作跳转
删除数据:
主页面:Main.php
<h1>主页面</h1>
<table width="100%" cellpadding="0" cellspacing="0" border="1">
<tr>
<td>代号</td>
<td>姓名</td>
<td>性别</td>
<td>民族</td>
<td>生日</td>
<td>操作</td>
</tr>
<?php
$db = new MySQLi("localhost","root","","mydb");
!mysqli_connect_error() or die("连接失败");
$sql = "select * from Info";
$result = $db->query($sql);
$attr = $result->fetch_all();
if($result)
{
foreach($attr as $v)
{
//处理性别
$sex = $v[2]?'男':'女';
//处理民族
$sql1 = "select name from Nation where Code ='{$v[3]}'";
$rnation = $db->query($sql1);
$attr1 = $rnation->fetch_assoc();
echo "<tr>
<td>{$v[0]}</td>
<td>{$v[1]}</td>
<td>{$sex}</td>
<td>{$attr1['name']}</td>
<td>{$v[4]}</td>
<td>
<a href='delete.php?code={$v[0]}'>删除</a>//特别注意的地方
<a href=''>修改</a>
</td>
</tr>";
}
}
?>
</table>
<div><a href="add.php">添加数据</a></div>
删除处理页面:delete.php
<?php
$code = $_GET["code"];
$db = new MySQLi("localhost","root","","mydb");
$sql = "delete from Info where Code = '{$code}'";
$result = $db->query($sql);
if($result)
{
header("location:Main.php");
}
else
{
echo "删除失败";
}
修改数据:
主页面:Main.php
<h1>主页面</h1>
<table width="100%" cellpadding="0" cellspacing="0" border="1">
<tr>
<td>代号</td>
<td>姓名</td>
<td>性别</td>
<td>民族</td>
<td>生日</td>
<td>操作</td>
</tr>
<?php
$db = new MySQLi("localhost","root","","mydb");
!mysqli_connect_error() or die("连接失败");
$sql = "select * from Info";
$result = $db->query($sql);
$attr = $result->fetch_all();
if($result)
{
foreach($attr as $v)
{
//处理性别
$sex = $v[2]?'男':'女';
//处理民族
$sql1 = "select name from Nation where Code ='{$v[3]}'";
$rnation = $db->query($sql1);
$attr1 = $rnation->fetch_assoc();
echo "<tr>
<td>{$v[0]}</td>
<td>{$v[1]}</td>
<td>{$sex}</td>
<td>{$attr1['name']}</td>
<td>{$v[4]}</td>
<td>
<a href='delete.php?code={$v[0]}'>删除</a>
<a href='update.php?code={$v[0]}'>修改</a>
</td>
</tr>";
}
}
?>
</table>
<div><a href="add.php">添加数据</a></div>
修改页面:update.php
<h1>数据修改</h1>
<?php
$code = $_GET["code"];
$db = new MySQLi("localhost","root","","mydb");
$ssql = "select * from Info where Code ='{$code}'";
$result = $db->query($ssql);
$arr = $result->fetch_row();//这个人的信息
?>
<form action="updatechuli.php" method="post">
<div>代号:<input type="hidden" name="code" value="<?php echo $arr[0]?>" /></div>
<div>姓名:<input type="text" name="name" value="<?php echo $arr[1]?>"/></div>
<div>性别:<input type="radio" name="sex" value="男" <?php echo $arr[2]?"checked='checked'":"" ?> />男
<input type="radio" name="sex" value="女" <?php echo $arr[2]?"":"checked='checked'" ?> />女
</div>
<div>民族:<select name="nation">
<?php
$sql = "select * from Nation";
$result = $db->query($sql);
$attr = $result->fetch_all();
foreach($attr as $v)
{
if($v[0]==$arr[3])
{
echo "<option selected='selected' value='{$v[0]}'>{$v[1]}</option>";
}
else
{
echo "<option value='{$v[0]}'>{$v[1]}</option>";
}
}
?>
</select>
</div>
<div>生日:<input type="text" name="birthday" value="<?php echo $arr[4] ?>"/></div>
<div><input type="submit" value="修改数据" /></div>
</form>
<div><a href="Main.php">主页面</a></div>
修改处理页面:updatechuli.php
<?php
$code = $_POST["code"];
$name = $_POST["name"];
$sex = $_POST["sex"];
$s =1;
if($sex=="女")
{
$s = 0;
}
$nation = $_POST["nation"];
$birthday = $_POST["birthday"];
$db = new MySQLi("localhost","root","","mydb");
$sql = "Update Info set Name='{$name}',Sex={$s},Nation='{$nation}',Birthday='{$birthday}' where Code='{$code}'";
$result = $db->query($sql);
if($result)
{
header("location:Main.php");
}
else
{
echo "修改失败";
}