php 数据库的增删改查
Posted 曉瀦
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 数据库的增删改查相关的知识,希望对你有一定的参考价值。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
$db= new mysqli("localhost","root","root","asd_8080");
//empty(mysqli_connect_error())?"":die("连接错误");
$sql="select * from user where is_delete=‘0‘";
$data= $db->query($sql)->fetch_all();
if (!empty($_POST)){
$name = $_POST[‘name‘];
$sex = $_POST[‘sex‘];
$birth_start=$_POST[‘birth_start‘];
$birth_end = $_POST[‘birth_end‘];
$sql .= !empty($name)? "and name like ‘%{$name}%‘":"";
$sql .= !empty($sex)? "and sex like ‘%{$sex}%‘":"";
}
?>
<form action="mxdx2.php" method="post">
<table>
<tr>
<td>姓名:<input type="text" name="name"></td>
<td>性别:
<select name="sex">
<option value="1">男</option>
<option value="0">女</option>
</select>
</td>
<td>
年龄:<input type="text" name="birth_start">---<input type="text" name="birth_end">
</td>
<td>
<input type="submit" value="查询">
</td>
</tr>
</table>
</form>
<form action="plsc.php" method="post">
<table border="1">
<form border="2">
<tr>
<td>ID</td>
<td>姓名</td>!
<td>性别</td>
<td>年龄</td>
<td>出生年月</td>
</tr>
<?php
foreach ($data as $i){
if ($i[2]== 1 ){
$i[2]="男";
}else if ($i[2]== 0 ){
$i[2]="女";
}else{
$i[2]="保密";
}
echo "<tr><td>{$i[0]}<input type=‘checkbox‘ name=‘ids[]‘ value=‘{$i[0]}‘></td><td>{$i[1]}</td><td>{$i[2]}</td><td>{$i[3]}</td><td>{$i[4]}</td><td><a href=‘mxdx3.php?id={$i[0]}‘>删除</a></td> <td><a href=‘edit.php?id={$i[0]}‘>修改</a></td></tr>";
}
?>
</table>
<input type="submit" value="批量删除">
</form>
<a href="add.php">新增用户</a>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form method="post" action="addpost.php">
<input type="text" name="name">
<input type="radio" name="sex" value="1" id="man" ><label for="man">男</label>
<input type="radio" name="sex" value="0" id="woman" ><label for="woman">女</label>
<input type="text" name="age" placeholder="年龄">
<input type="text" name="create_time" placeholder="出生年月">
<input type="submit" value="提交">
</form>
</body>
</html>
<?php
$name = $_POST[‘name‘];
$sex = $_POST[‘sex‘];
$age=$_POST[‘age‘];
$birthday = $_POST[‘create_time‘];
$db = new Mysqli("localhost","root","root","asd_8080");
$sql = "INSERT INTO user VALUES (NULL ,‘{$name}‘,{$sex},‘{$age}‘,{$birthday},‘0‘)";
if($db->query($sql)){
header("location:mxdx2.php");
}else{
header("location:add.php");
}
<?php
$id = $_GET[‘id‘];
$db= new Mysqli("localhost","root","root","asd_8080");
mysqli_connect_error()?die("连接出错"):"";
$sql = "delete from USER WHERE id = {$id}";
$sql2 = "update user set is_delete =‘1‘ WHERE id ={$id}";
if ($db->query($sql2)){
header("location:mxdx2.php");
}
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form action="editpost.php" method="post">
<input type="hidden" name="id" value="<?php echo $data[‘id‘]?>">
<input type="text" name="neme" value="<?php echo $data[‘neme‘]?>">
<input type="radio" name="sex" value="1" <?php if ($data[‘sex‘]==1){echo ‘checked‘;}?> id="man" ><label for="man">男</label>
<input type="radio" name="sex" value="0" <?php if ($data[‘sex‘]==0){echo ‘checked‘;}?> id="woman" ><label for="woman">女</label>
<input type="text" name="age" value="<?php echo $data[‘age‘]?>" placeholder="年龄">
<input type="text" name="create_time" placeholder="出生年月" value="<?php echo $data[‘create_time‘]?>">
<input type="submit" value="提交">
</form>
</body>
</html>
<?php
$id=$_POST[‘id‘];
$name = $_POST[‘neme‘];
$sex = $_POST[‘sex‘];
$age=$_POST[‘age‘];
$birthday = $_POST[‘create_time‘];
var_dump($name);
$db= new Mysqli("localhost","root","root","asd_8080");
$sql = "update `user` set neme=‘{$name}‘,sex=‘{$sex}‘,age ={$age},create_time=‘{$birthday}‘WHERE id={$id}";
if ($db->query($sql)){
echo "执行成功";
}
以上是关于php 数据库的增删改查的主要内容,如果未能解决你的问题,请参考以下文章