以往也学习过数据库的增删改查,是通过在数据库中写SQL语句实现的
而近期则是通过在php页面写代码操作数据库,在网页的页面上可以直接点击删除或添加按钮直接实现数据的添加或删除
php代码相当于一个连接数据库和网页的桥梁,也是php实际应用的体现
以一张表为例,实现增加,修改,删除的功能:
这是主界面,将数据库中的数据以表格的形式显现,并增加了添加,修改,删除数据的链接页面地址,通过点击进入新的界面,实现功能:
<body> <h1>主页面</h1> <table width="100%" border="1" cellspacing="0" cellpadding="0"> <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(); foreach($attr as $v) { $sex = $v[2]?‘男‘:‘女‘; //处理民族名称 $sqln = "select name from Nation where Code=‘$v[3]‘"; $rnation = $db->query($sqln); $attrn = $rnation->fetch_row(); echo "<tr> <td>{$v[0]}</td> <td>{$v[1]}</td> <td>{$sex}</td> <td>{$attrn[0]}</td> <td>{$v[4]}</td> <td> <a href=‘DeleteOne.php?code={$v[0]}‘>删除</a> <a href=‘Updateone.php?code={$v[0]}‘>修改</a> </td> </tr>"; } ?> </table> <div><a href="Addone.php">添加数据</a></div> </body>
这是添加数据界面,做出基本框架:
<body> <h1>添加数据</h1> <form action="Addchu.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"); !mysqli_connect_error() or die("连接错误"); $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"]; $s = 1; if($sex =="女") { $s = 0; } $nation = $_POST["nation"]; $birthday = $_POST["birthday"]; $db = new MySQLi("localhost","root","","mydb"); !mysqli_connect_error() or die("连接失败"); $sql = "insert into Info values(‘{$code}‘,‘{$name}‘,{$s},‘{$nation}‘,‘{$birthday}‘)"; $result = $db->query($sql); if($result) { header("location:Addone.php"); } else { echo "添加失败!"; }
这是修改界面,当点击修改按钮后会直接出现相关内容,与数据库相连,修改完成提交后,进入数据处理页面,对数据库中的数据进行修改
<body> <h1>修改数据</h1> <?php $code = $_GET["code"]; $db = new MySQLi("localhost","root","","mydb"); $sinfo ="select *from Info where Code=‘{$code}‘"; $r = $db->query($sinfo); $arr = $r->fetch_row(); ?> <form action="Addchu.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" value="男" name="sex" <?php echo $arr[2]?"checled=checked":"" ?>/>男 <input type="radio" value="女" name="sex" <?php echo $arr[2]?"":"checked=checked" ?>/>女 </div> <div>民族: <select name="nation"> <?php !mysqli_connect_error() or die("连接错误"); $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> </body>
修改数据页面:
<body> <h1>修改数据</h1> <?php $code = $_GET["code"]; $db = new MySQLi("localhost","root","","mydb"); $sinfo ="select *from Info where Code=‘{$code}‘"; $r = $db->query($sinfo); $arr = $r->fetch_row(); ?> <form action="Addchu.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" value="男" name="sex" <?php echo $arr[2]?"checled=checked":"" ?>/>男 <input type="radio" value="女" name="sex" <?php echo $arr[2]?"":"checked=checked" ?>/>女 </div> <div>民族: <select name="nation"> <?php !mysqli_connect_error() or die("连接错误"); $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> </body>
删除页面:删除相关数据
<?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:One.php"); } else { echo "删除失效"; }
时间: 2024-11-15 01:29:48