显示数据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();
}
?>