PDO操作MySQL数据库Demo

显示数据Test.php:

<?php
$dsn="mysql:host=localhost;dbname=school";
try{
	//链接数据库
	$pdo=new PDO($dsn,"root","123456");
	//执行sql语句
	 $sql="SELECT * FROM `websites`;";
	 $stmt=$pdo->prepare($sql);//预处理
	 $stmt->execute();//执行预处理
	 //处理结果集
	 $rs=$stmt->fetchAll();
	//var_dump($rs);
	
}catch(PDOException $e){
	echo $e->getMessage();
}
?>
<!DOCTYPE html>
<html lang="zh">
<head>
	<meta charset="UTF-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1.0" />
	<meta http-equiv="X-UA-Compatible" content="ie=edge" />
	<title>Document</title>
</head>
<body>
	<a href="add.php">添加</a>
	<table border="" cellspacing="" cellpadding="">
		<tr>
			<th>ID</th>
			<th>名称</th>
			<th>URL地址</th>
			<th>aleax值</th>
			<th>国家</th>
			<th>操作</th>
		</tr>
		<?php foreach ($rs as $key => $value) {
		?>
		<tr>
			<td><?php echo $rs[$key]['id']; ?></td>
			<td><?php echo $rs[$key]['name']; ?></td>
			<td><?php echo $rs[$key]['url']; ?></td>
			<td><?php echo $rs[$key]['aleax']; ?></td>
			<td><?php echo $rs[$key]['country']; ?></td>
			<td>
				<a href="update.php?id=<?php echo $rs[$key]['id']; ?>">修改</a>
				<a href="del.php?id=<?php echo $rs[$key]['id']; ?>">删除</a>
			</td>
		</tr>
			
		<?php 
		} ?>
		
	</table>
</body>
</html>

添加add.php:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
		<form action="addAction.php" method="post">
			<table border="1" cellspacing="0" cellpadding="5">
				<tr>
					<td>名称</td>
					<td><input type="text" name="name" id="name" value="" /> </td>
				</tr>
				<tr>
					<td>URL</td>
					<td><input type="text" name="url" id="url" value="" /> </td>
				</tr>
				<tr>
					<td>aleax</td>
					<td><input type="text" name="aleax" id="aleax" value="" /> </td>
				</tr>
				<tr>
					<td>country</td>
					<td><input type="text" name="country" id="country" value="" /> </td>
				</tr>
				<tr>
					<td colspan="2">
						<input type="submit" name="" id="" value="添加" />
					</td>
				</tr>
			</table>
		</form>
	</body>
</html>

添加数据处理addAction.php:

<?php
$name=$_POST['name'];
$url=$_POST['url'];
$aleax=$_POST['aleax'];
$country=$_POST['country'];
//连接数据库
$dns="mysql:host=localhost;dbname=school";
try{
	//链接PDO
	$pdo=new PDO($dns,"root",'123456');
	//预处理
	$sql="insert into websites values(null,?,?,?,?);";
	$stmt=$pdo->prepare($sql);
	$rs=$stmt->execute(array($name,$url,$aleax,$country));
	if($rs){
		header("location:Test.php");
	}else{
		echo "添加失败";
	}
}catch(PDOException $e){
	echo $e->getMessage();
}
?>

删除del.php:

<?php
$id=$_GET['id'];
$dns="mysql:host=localhost;dbname=school";
try{
	$pdo=new PDO($dns,"root",'123456');
	$sql="delete from websites where id=?;";
	$stmt=$pdo->prepare($sql);
	$rs=$stmt->execute(array($id));
	if($rs){
		header("location:Test.php");
	}else{
		echo "删除失败";
	}
}catch(PDOException $e){
	echo $e->getMessage();
}

?>

更新update.php:

<?php
$id=$_GET['id'];

$dns="mysql:host=localhost;dbname=school";
try{
	//链接数据库
	$pdo=new PDO($dns,"root","123456");
	//执行sql语句
	 $sql="SELECT * FROM `websites` where id=?;";
	 $stmt=$pdo->prepare($sql);//预处理
	 $stmt->execute(array($id));
	 $rs=$stmt->fetch();
	 //var_dump($rs);
	
}catch(PDOException $e){
	echo $e->getMessage();
}

?>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
		<form action="updateAction.php?id=<?php echo $id; ?>" method="post">
			<table border="1" cellspacing="0" cellpadding="5">
				<tr>
					<td>名称</td>
					<td><input type="text" name="name" id="name" value="<?php echo $rs['name'] ?>" /> </td>
				</tr>
				<tr>
					<td>URL</td>
					<td><input type="text" name="url" id="url" value="<?php echo $rs['url'] ?>" /> </td>
				</tr>
				<tr>
					<td>aleax</td>
					<td><input type="text" name="aleax" id="aleax" value="<?php echo $rs['aleax'] ?>" /> </td>
				</tr>
				<tr>
					<td>country</td>
					<td><input type="text" name="country" id="country" value="<?php echo $rs['country'] ?>" /> </td>
				</tr>
				<tr>
					<td colspan="2">
						<input type="submit" name="" id="" value="修改" />
					</td>
				</tr>
			</table>
		</form>
	</body>
</html>

更新数据处理updateAction.php:

<?php
$name=$_POST['name'];
$url=$_POST['url'];
$aleax=$_POST['aleax'];
$country=$_POST['country'];
//连接数据库
$dns="mysql:host=localhost;dbname=school";
try{
	//链接PDO
	$pdo=new PDO($dns,"root",'123456');
	//预处理
	$sql="insert into websites values(null,?,?,?,?);";
	$stmt=$pdo->prepare($sql);
	$rs=$stmt->execute(array($name,$url,$aleax,$country));
	if($rs){
		header("location:Test.php");
	}else{
		echo "添加失败";
	}
}catch(PDOException $e){
	echo $e->getMessage();
}
?>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值