php简单的数据增删改查

列表页代码:

<?php
//连接数据库
$db = new MySQLi('localhost','root','','z_1032');
!mysql_connect_error() or die('连接失败');
$db->query('set names utf8');


//sql条件
$where = 'where 1=1';
if(!empty($_POST['name'])){
	$name = $_POST['name'];
	$where .= " and name like '%$name%'";
}
if(!empty($_POST['tel'])){
	$tel = $_POST['tel'];
	$where .= " and tel = '$tel'";
}


//查询数据
$sql = "select * from lxr_lianxiren where 1 = 1 ".$where;
$res = $db->query($sql);//执行sql语句
$arr = $res->fetch_all();//结果集返回数组,索引数组



//查询数据
$sql = "select * from lxr_groups";
$res = $db->query($sql);
$attr = array();
while($row=$res->fetch_assor()){//fetch_assor()返回一行数据  关联数组
	$attr[$row['id']] = $row['name'];
}

?>





<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>列表页</title>
</head>

<body>
<a href="add.php"><button>添加</button></a>
<form action="list.php" method="post">
	姓名: <input type="text" name="name">
	手机号: <input type="text" name="tel">
	<button>查询</button>
</form>
<table width="80%" border="1" cellpadding="0" cellspacing="0">
	<tr>
		<th>id</th>
		<th>姓名</th>
		<th>手机号</th>
		<th>分组</th>
		<th>操作</th>
	</tr>
	<?php foreach($arr as $v){ ?>
		<tr>
			<td><?php echo $v[0]; ?></td>
			<td><?php echo $v[1]; ?></td>
			<td><?php echo $v[2]; ?></td>
			<td><?php echo $attr[$v[3]]; ?></td>
			<td>
				<a href="php.php?<?php echo $v[0]; ?>">
					<button>删除</button>
				</a>
				<a href="edit.php?<?php echo $v[0]; ?>">
					<button>修改</button>
				</a>
			</td>
		</tr>
	<?php } ?>
</table>
</body>
</html>

  添加页代码:

<?php
//连接数据库
$db = new MySQLi('localhost','root','','z_1032');
!mysql_connect_error() or die('连接失败');
$db->query('set names utf8');


//查询数据
$sql = "select * from lxr_groups";
$res = $db->query($sql);
$attr = array();
while($row=$res->fetch_assor()){//fetch_assor()返回一行数据  关联数组
	$attr[$row['id']] = $row['name'];
}


?>





<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>

<body>
<form action="php.php?type=add" method="post">
	联系人: <input type="text" name="name"> <br>
	tel: <input type="text" name="tel"> <br>
	分组: <select name="groupid" id="">
	  		<?php foreach($attr as $k=>$v){ ?>
		  		<option value="<?php echo $k; ?>"><?php echo $v; ?></option>
		  	<?php } ?>
	      </select> <br>
	<button>提交</button>
</form>
</body>
</html>

  修改页代码:

<?php
//连接数据库
$db = new MySQLi('localhost','root','','z_1032');
!mysql_connect_error() or die('连接失败');
$db->query('set names utf8');


$id = $_GET['id'];
//查询数据
$sql = "select * from lxr_lianxiren where id = $id";
$res = $db->query($sql);
$arr=$res->fetch_assor();


//查询数据
$sql = "select * from lxr_groups";
$res = $db->query($sql);
$attr = array();
while($row=$res->fetch_assor()){//fetch_assor()返回一行数据  关联数组
	$attr[$row['id']] = $row['name'];
}


?>





<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>

<body>
<form action="php.php?type=update" method="post">
	<input type="hidden" name="id" value="<?php echo $id; ?>">
	联系人: <input type="text" name="name" value="<?php echo $arr['name']; ?>"> <br>
	tel: <input type="text" name="tel" value="<?php $arr['tel']; ?>"> <br>
	分组: <select name="groupid" id="">
  		
	  		<?php foreach($attr as $k=>$v){
				if($k == $arr['groupid']){
					echo "<option value='$k' selected>$v</option>";
				}else{
					echo "<option value='$k'>$v</option>";
				}
			} ?>
	      </select> <br>
	<button>提交</button>
</form>
</body>
</html>

  后台php代码:

<?php
//连接数据库
$db = new MySQLi('localhost','root','','z_1032');
!mysql_connect_error() or die('连接失败');
$db->query('set names utf8');

$type = $_REQUEST['type'];//通过这个类型判断修改还是删除
switch($type){
	case 'update':
		$id = $_POST['id'];
		$name = $_POST['name'];
		$tel = $_POST['tel'];
		$groupid = $_POST['groupid'];
		
		$sql = "update lxr_lianxiren set name = '$name', tel = '$tel',groupid = '$groupid' where id = $id";
		$res = $db->query($sql);
		if($res){
			header('localhost:list.php');//header()页面跳转
		}else{
			echo "修改失败";
			header('refresh:3;url=list.php?id=10');
		}
		break;
		break;
	case 'add':
		$name = $_POST['name'];
		$tel = $_POST['tel'];
		$groupid = $_POST['groupid'];
		
		$sql = "insert into lxr_lianxiren(name,tel,groupid) values('$name','$tel','$groupid')";
		$res = $db->query($sql);
		if($res){
			header('localhost:list.php');//header()页面跳转
		}else{
			echo "添加失败";
			header('refresh:3;url=list.php?id=10');
		}
		break;
	default:
		//接收值
		$id = $_GET['id'];
		//执行sql语句,删数据
		$sql = "delete from lxr_lianxiren where id = $id";
		$res = $db->query($sql);

		if($res){
			header('localhost:list.php');//header()页面跳转
		}else{
			echo "删除失败";
			header('refresh:3;url=list.php?id=10');
		}
		break;
}


?>







<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>

<body>
</body>
</html>

  

转载于:https://www.cnblogs.com/1500418882qqcom/p/10205981.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值