PHPMYSQLI实现简单的增删改查功能(初学者)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHPMYSQLI实现简单的增删改查功能(初学者)相关的知识,希望对你有一定的参考价值。
<title>index.php</title> <?php #连接数据库 $conn = mysqli_connect("localhost","root",""); #判断是否连接成功 if(!$conn){ echo "失败"; } //选择数据库 mysqli_select_db($conn,"bbs"); //准备sql语句 $sql = "select * from bbs_user"; //发送sql语句 $obj = mysqli_query($conn,$sql); echo "<center>"; echo "<table border = 1 cellspacing = '0' cellpadding = '10'>"; echo "<th>编号</th><th>姓名</th><th>密码</th><th>地址</th><th>性别</th><th>年龄</th><th>操作</th>"; while($row = mysqli_fetch_assoc($obj)){ echo "<tr>"; echo '<td>'.$row['id'].'</td>'; echo '<td>'.$row['username'].'</td>'; echo '<td>'.$row['password'].'</td>'; echo '<td>'.$row['address'].'</td>'; echo '<td>'.$row['sex'].'</td>'; echo '<td>'.$row['age'].'</td>'; echo '<td><a href = "del.php?id='.$row['id'].'">删除</a>/<a href = "update.php?id='.$row['id'].'">修改</a></td>'; echo "</tr>"; } echo "</table>"; echo "<a href = 'add.php'>添加</a>"; echo "<center>"; //关闭连接 mysqli_close($conn); ?>
<title>del.php</title> <?php $id =$_GET['id']; $link = mysqli_connect('localhost','root',''); if(!$link){ exit('连接失败'); } mysqli_select_db($link,'bbs'); $sql = "delete from bbs_user where id =$id"; $result = mysqli_query($link,$sql); if($result && mysqli_affected_rows($link)){ echo "删除成功<a href = 'index.php'>返回</a>"; }else{ echo "删除失败"; } mysqli_close($link); ?>
更改数据:
<?php $id = $_GET['id']; $link = mysqli_connect('localhost','root',''); if(!$link){ exit('连接失败'); } mysqli_select_db($link,'bbs'); $sql = "select * from bbs_user where id =$id"; $obj = mysqli_query($link,$sql); $row = mysqli_fetch_assoc($obj); ?> <form action="doupdate.php?"> <!--用隐藏式来获取id,其他的目前不行--> <input type="hidden" name="id" value = "<?php echo $id;?>" /> <table border = 0 cellpadding="10" cellspacing="0"> <tr><td>编号:</td><td><?php echo $row['id'];?></td></tr> <tr><td>用户名:</td><td><input type="text" name = "username" value="<?php echo $row['username'];?>" /></td></tr> <!--<tr><td>密码:</td><td><input type="text" name = "password" value="<?php echo $row['password'];?>" /></td></tr>--> <tr><td>密码:</td><td><?php echo $row['password'];?></td></tr> <tr><td>地址:</td><td><input type="text" name="address" value="<?php echo $row['address'];?>" /></td></tr> <tr><td>性别:</td><td><input type="text" name="sex" value="<?php echo $row['sex'];?>" /></td></tr> <tr><td>年龄:</td><td><input type="text" name="age" value="<?php echo $row['age'];?>" /><br /> <tr><td colspan="2" align="center"><input type="submit" name="" value="提交" /></td></tr> </table> </form>
执行更改:
<?php $id = $_GET['id']; $username = $_GET['username']; #$password = $_GET['password']; $address = $_GET['address']; $sex = $_GET['sex']; $age = $_GET['age']; $link = mysqli_connect('localhost','root',''); if(!$link){ exit('连接失败'); } mysqli_select_db($link,'bbs'); $sql = "update bbs_user set username = '$username', address = '$address', sex = '$sex', age = '$age' where id =$id"; $obj = mysqli_query($link,$sql); if($obj && mysqli_affected_rows($link)){ echo "修改成功<a href = 'index.php'>返回</a>"; }else{ echo "修改失败"; } mysqli_close($link); ?>
<form action = 'doadd.php' method="get"> 编号:<input type="text" name="id" value=""/><br /> 姓名:<input type="text" name="username" value=""/><br /> 密码:<input type="text" name="password" value=""/><br /> 地址:<input type="text" name="address" value=""/><br /> 性别:<input type="radio" name="sex" value = "男"/>男 <input type="radio" name="sex" value="女" />女<br /> 年龄:<input type="text" name="age" value=""/><br /> <input type="submit" value="提交" /> </form>
<?php //获取添加的数据信息 $id = $_GET['id']; $username = $_GET['username']; $password = md5($_GET['password']);//加密 $address = $_GET['address']; $sex = $_GET['sex']; $age = $_GET['age']; //连接数据库 $link = mysqli_connect('localhost' , 'root' , ''); //判断是否连接成功 if(!$link){ exit('连接数据库失败'.mysqli_connect_error()); } //选择数据库 mysqli_select_db($link , 'bbs'); //准备sql语句 $sql = "insert into bbs_user(id,username,password,address,sex,age) values('$id','$username','$password','$address','$sex','$age')"; //发送sql语句 $obj = mysqli_query($link , $sql); if($obj){ echo "添加成功<a href = 'index.php'>返回</a>"; }else{ echo "添加失败"; } mysqli_close($link); ?>
以上是关于PHPMYSQLI实现简单的增删改查功能(初学者)的主要内容,如果未能解决你的问题,请参考以下文章
SpringBoot+Vue+Axios+BootStrap实现图书的增删改查功能