<title>index.PHP</title> <?php #连接数据库
<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);
?>
在基础上加了个查询功能:
跳转到select.php中:
<form action="doselect.php" >
请输入您要查询的编号:<input type="text" name="id" />
<input type="submit" value="查询" />
</form>
执行:
执行的代码段是:
<?php
$id = $_GET['id'];
$conn = mysqli_connect('localhost' , 'root' , '');
if(!$conn){
exit('连接失败');
}
mysqli_select_db($conn,'bbs');
$sql = "select * from bbs_user where id=$id";
$result = 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>";
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);
?>
查到到结果如下图:
--结束END--
本文标题: PHP、MYSQLI实现简单的增、删、改、查功能(初学者)
本文链接: https://lsjlt.com/news/44220.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-10-23
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0