php 用面向对象的方法对数据库增删改查

时间:2021-07-16 10:36:42

主页面

<body>
<h1>主页面</h1>
<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");
//判断是否出错
!mysqli_connect_error() or die("连接失败!");
//写SQL语句
$sql = "select * from Info";
//执行SQL语句
$result = $db->query($sql); //读取数据
$attr = $result->fetch_all(); //遍历数组,显示
foreach($attr as $v)
{ $sex = $v[2]?"男":"女"; $sql = "select Name from Nation where Code = '{$v[3]}'";
$r = $db->query($sql);
$a = $r->fetch_row(); echo "
<tr>
<td>{$v[0]}</td>
<td>{$v[1]}</td>
<td>{$sex}</td>
<td>{$a[0]}</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>
<a href="add.php">添加数据</a>
</body>

添加

<body>
<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" value="1" name="sex" />男 <input type="radio" value="0" name="sex" />女</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>
<input type="submit" value="添加" />
</form>
<br />
<a href="main.php">返回主页面</a>
</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}')"; $r = $db->query($sql); if($r)
{
header("location:add.php");
}
else
{
echo "添加失败!";
}

删除

<?php
$code = $_GET["code"]; $db = new MySQLi("localhost","root","123","mydb");
$sql = "delete from Info where Code = '{$code}'"; if($db->query($sql))
{
header("location:main.php");
}
else
{
echo "删除失败!";
}

修改

<body>
<h1>修改数据</h1> <?php
$code = $_GET["code"]; $db = new MySQLi("localhost","root","123","mydb");
$sql = "select * from Info where Code = '{$code}'";
$result = $db->query($sql); $attr = $result->fetch_row();
?> <form action="updatechuli.php" method="post">
<div>代号:
<input type="text" readonly="readonly" name="code" value="<?php echo $attr[0] ?>" />
</div>
<div>姓名:
<input type="text" name="name" value="<?php echo $attr[1] ?>" />
</div>
<div>性别:
<input type="radio" value="1" name="sex" <?php echo $attr[2]?"checked='checked'":""; ?> />男
<input type="radio" value="0" name="sex" <?php echo $attr[2]?"":"checked='checked'"; ?> />女
</div>
<div>民族:
<select name="nation">
<?php
$db = new MySQLi("localhost","root","123","mydb");
$sql = "select * from Nation";
$result = $db->query($sql);
$attr1 = $result->fetch_all();
foreach($attr1 as $v)
{
//判断民族是否选中
if($attr[3] == $v[0])
{
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 $attr[4] ?>" />
</div>
<input type="submit" value="修改" />
</form>
<br />
<a href="main.php">返回主页面</a>
</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 = "update Info set Name='{$name}',Sex={$sex},Nation='{$nation}',Birthday='{$birthday}' where Code='{$code}' "; $r = $db->query($sql); if($r)
{
header("location:main.php");
}
else
{
echo "修改失败!";
}