mysql增删改处理

首页:

<h1>首页</h1>
<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
<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);
if($result)
{
$attr = $result->fetch_all();
foreach ($attr as $a)
{
$sex = $a[2]?"男":"女";
$sql1 = "select Name from nation where Code='{$a[3]}'";
$re1 = $db->query($sql1);
$attr1 = $re1->fetch_assoc();
echo "<tr>
<td>{$a[0]}</td>
<td>{$a[1]}</td>
<td>{$sex}</td>
<td>{$attr1['Name']}</td>
<td>{$a[4]}</td>
<td><a href='delete.php?code=$a[0]'>删除</a></td>
<td><a href='updeat.php?code=$a[0]'>修改</a></td>
</tr>";
}
}
?>

</table>
<div><a href="add.php">添加数据</a></div>

添加数据:

<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="男" name="sex" />男
<input type="radio" value="女" name="sex" />女
</div>
<div>民族:
<select name="nation">
<?php

$db = new MySQLi("localhost","root","","mydb");
$sql = "select * from Nation";
$r = $db->query($sql);
$attr = $r->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>

增加处理:

<?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");
!mysqli_connect_error() or die("连接失败!");
$c="insert into info values('{$code}','{$name}',{$s},'{$nation}','{$birthday}')";
$r = $db->query($c);
if($r)
{
header("location:add.php");
}
else
{
echo "添加失败!";
}

修改:

h1>修改数据</h1>
<?php
$code1 = $_GET["code"];
$db = new MySQLi("localhost","root","","mydb");
!mysqli_connect_error() or die ("连接失败!");
$sql = "select * from info where code='{$code1}'";
$r = $db->query($sql);
$attr = $r->fetch_row();
?>
<form action="updeatchuli.php" method="post">
<div><input type="hidden" value="<?php echo $attr[0] ?>" name="code" /></div>
<div>姓名:<input type="text" value="<?php echo $attr[1] ?>" name="name" /></div>
<div>性别:
<input type="radio" value="男" name="sex" <?php echo $attr[2]?"checked='checked'":"" ?>/>男
<input type="radio" value="女" name="sex" <?php echo $attr[2]?"":"checked='checked'" ?>/>女
</div>
<div>民族:
<select name="nation">
<?php


$sql1 = "select * from Nation";
$r1 = $db->query($sql1);
$attr1 = $r1->fetch_all();
foreach($attr1 as $v)
{
if($v[0]==$attr[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" value="<?php echo $attr[4] ?>" name="birthday" /></div>
<div><input type="submit" value="修改数据" /></div>
</form>
<div><a href="main.php">主页面</a></div>

修改处理:

<?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");
!mysqli_connect_error() or die("连接失败!");
$c="update info set name='{$name}',sex='{$s}',nation='{$nation}',birthday='{$birthday}' where code='{$code}'";
$r = $db->query($c);
if($r)
{
header("location:updeat.php");
}
else
{
echo "修改失败!";
}

删除:

<?php
$code = $_GET["code"];
$db = new MySQLi("localhost","root","","mydb");
!mysqli_connect_error() or die("连接失败!");
$sql = "delete from info where code ='{$code}'";
$r = $db->query($sql);
if($r)
{
header("location:sy.php.php");
}
else
{
echo "删除失败!";
}

原文地址:https://www.cnblogs.com/1116zsc/p/5470526.html