• ADADADADAD

    PHP、MYSQLI实现简单的增、删、改、查功能(初学者)[ mysql数据库 ]

    mysql数据库 时间:2024-12-24 19:13:28

    作者:文/会员上传

    简介:

    <title>index.php</title><?php#连接数据库$conn=mysqli_connect("localhost","root","");#判断是否连接成功if(!$conn){echo"失败";}//选择数据库mysqli_select_db($conn,"

    以下为本文的正文内容,内容仅供参考!本站为公益性网站,复制本文以及下载DOC文档全部免费。

    <title>index.php</title><?php#连接数据库$conn=mysqli_connect("localhost","root","");#判断是否连接成功if(!$conn){echo"失败";}//选择数据库mysqli_select_db($conn,"bbs");//准备sql语句$sql="select*frombbs_user";//发送sql语句$obj=mysqli_query($conn,$sql);echo"<center>";echo"<tableborder=1cellspacing='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><ahref="del.php?id='.$row['id'].'">删除</a>/<ahref="update.php?id='.$row['id'].'">修改</a></td>';echo"</tr>";}echo"</table>";echo"<ahref='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="deletefrombbs_userwhereid=$id";$result=mysqli_query($link,$sql);if($result&&mysqli_affected_rows($link)){echo"删除成功<ahref='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*frombbs_userwhereid=$id";$obj=mysqli_query($link,$sql);$row=mysqli_fetch_assoc($obj);?><formaction="doupdate.php?"><!--用隐藏式来获取id,其他的目前不行--><inputtype="hidden"name="id"value="<?phpecho$id;?>"/><tableborder=0cellpadding="10"cellspacing="0"><tr><td>编号:</td><td><?phpecho$row['id'];?></td></tr><tr><td>用户名:</td><td><inputtype="text"name="username"value="<?phpecho$row['username'];?>"/></td></tr><!--<tr><td>密码:</td><td><inputtype="text"name="password"value="<?phpecho$row['password'];?>"/></td></tr>--><tr><td>密码:</td><td><?phpecho$row['password'];?></td></tr><tr><td>地址:</td><td><inputtype="text"name="address"value="<?phpecho$row['address'];?>"/></td></tr><tr><td>性别:</td><td><inputtype="text"name="sex"value="<?phpecho$row['sex'];?>"/></td></tr><tr><td>年龄:</td><td><inputtype="text"name="age"value="<?phpecho$row['age'];?>"/><br/><tr><tdcolspan="2"align="center"><inputtype="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="updatebbs_usersetusername='$username',address='$address',sex='$sex',age='$age'whereid=$id";$obj=mysqli_query($link,$sql);if($obj&&mysqli_affected_rows($link)){echo"修改成功<ahref='index.php'>返回</a>";}else{echo"修改失败";}mysqli_close($link);?>

    添加数据:

    <formaction='doadd.php'method="get">编号:<inputtype="text"name="id"value=""/><br/>姓名:<inputtype="text"name="username"value=""/><br/>密码:<inputtype="text"name="password"value=""/><br/>地址:<inputtype="text"name="address"value=""/><br/>性别:<inputtype="radio"name="sex"value="男"/>男<inputtype="radio"name="sex"value="女"/>女<br/>年龄:<inputtype="text"name="age"value=""/><br/><inputtype="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="insertintobbs_user(id,username,password,address,sex,age)values('$id','$username','$password','$address','$sex','$age')";//发送sql语句$obj=mysqli_query($link,$sql);if($obj){echo"添加成功<ahref='index.php'>返回</a>";}else{echo"添加失败";}mysqli_close($link);?>

    在基础上加了个查询功能:

    跳转到select.php中:

    <formaction="doselect.php">请输入您要查询的编号:<inputtype="text"name="id"/>&nbsp;<inputtype="submit"value="查询"/></form>

    执行:

    执行的代码段是:

    <?php$id=$_GET['id'];$conn=mysqli_connect('localhost','root','');if(!$conn){exit('连接失败');}mysqli_select_db($conn,'bbs');$sql="select*frombbs_userwhereid=$id";$result=mysqli_query($conn,$sql);echo"<center>";echo"<tableborder=1cellspacing='0'cellpadding='10'>";echo"<th>编号</th><th>姓名</th><th>密码</th><th>地址</th><th>性别</th><th>年龄</th>";while($row=mysqli_fetch_assoc($result)){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"</tr>";}echo"</table>";echo"<center>";mysqli_close($conn);?>

    查到到结果如下图:

    PHP、MYSQLI实现简单的增、删、改、查功能(初学者).docx

    将本文的Word文档下载到电脑

    推荐度:

    下载
    热门标签: phpmysqli