您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章给大家分享的是有关如何使用PHP连接数据库并实现用户数据的增删改查的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
main页面(主页面)
<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","","mydb"); if(mysqli_connect_error()){ die("连接失败"); } $sql = "select * from Info"; $result = $db->query($sql); $attr = $result->fetch_all(); foreach($attr as $v) { $sex = $v[2]? '男':'女'; //三元运算符判断性别 $sql = "select Name from Nation where Code ='$v[3]'"; $result = $db ->query($sql); $attr = $result->fetch_assoc(); echo "<tr> <td>{$v[0]}</td> <td>{$v[1]}</td> <td>{$sex}</td> <td>{$attr['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" rel="external nofollow" >添加数据</a> </div>
Add(添加数据页面)
<h2>添加数据</h2> <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"); if(mysqli_connect_error()){ die("连接失败"); } $sql = "select * from Nation"; $r = $db->query($sql); $att = $r->fetch_all(); foreach($att 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>
AddChuLi页面(添加数据处理页面)
<?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"); if(mysqli_connect_error()){ die("连接失败"); } $sql = "insert into Info values('{$code}','{$name}','{$s}','{$nation}','{$birthday}')"; //添加数据语句 $result = $db->query($sql); if($result) { header("location:main.php"); //php跳转页面方式 } else { echo "添加失败!"; }
DeleteChuLi页面(删除数据处理页面)
<?php $code = $_GET["code"]; $db = new MySQLi("localhost","root","","mydb"); if(mysqli_connect_error()){ die("连接失败"); } $sql = "delete from Info where Code ='{$code}'"; //删除语句 $r = $db->query($sql); if($r) { header("location:main.php"); } else { echo "删除失败!"; }
Update页面(修改数据页面)
<h2>修改数据</h2> <?php $code = $_GET["code"]; $db = new MySQLi("localhost","root","","mydb"); if(mysqli_connect_error()){ die("连接失败"); } $sql1 = "select * from Info where Code='{$code}'"; $r1 = $db->query($sql1); $att1 = $r1->fetch_row(); ?> <form action="UpdateChuLi.php" method="post"> <div>代号:<input type="hidden" name="code" value="<?php echo $att1[0] ?>" /></div> <div>姓名:<input type="text" name="name" value="<?php echo $att1[1] ?>" /></div> <div>性别: <input type="radio" value="男" name="sex" <?php echo $att1[2] ? "checked='checked'" : ""; ?> />男 <input type="radio" value="女" name="sex" <?php echo $att1[2] ? "" : "checked='checked'"; ?> />女 </div> <div>民族: <select name="nation"> <?php $db = new MySQLi("localhost","root","","mydb"); if(mysqli_connect_error()){ die("连接失败"); } $sql = "select * from Nation"; //查询nation一组数据 $r = $db->query($sql); $att = $r->fetch_all(); foreach($att as $v) { if($att1[3]==$v[0]) { echo "<option value='{$v[0]}' selected='selectec' >{$v[1]}</option>"; } else { echo "<option value='{$v[0]}'>{$v[1]}</option>"; } } ?> </select> </div> <div>生日:<input type="text" name="birthday" value="<?php echo $att1[4] ?>"/></div> <div><input type="submit" value="修改数据" /></div> </form>
ateChuLi页面(修改数据处理页面)
<?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"); if(mysqli_connect_error()){ die("连接失败"); } $sql = "update Info set Name='{$name}',Sex={$s},Nation='{$nation}',Birthday='{$birthday}' where Code='{$code}'"; //修改数据语句 $r = $db->query($sql); if($r) { header("location:main.php"); } else { echo "修改失败!"; }
感谢各位的阅读!关于“如何使用PHP连接数据库并实现用户数据的增删改查”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。