1. 程式人生 > >php簡單的資料增刪改查

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>