数据库功能:
1.连接数据库,并将数据输出到列表页
代码
<?php
// 1.bindColmun 将结果绑定到指定变量上
// try {
// $pdo = new PDO('mysql:host=localhost;dbname=user','user','user123');
// } catch (PDOException $e) {
// echo '数据链接失败:'. $e->getMessage();
// }
// $pre = $pdo->prepare('SELECT * FROM `user`');
// $exe = $pre->execute();
// $pre->bindColumn('name',$name);
// while($pre->fetch(PDO::FETCH_ASSOC)){
// echo '姓名:'.$name;
// echo '<br>';
// }
// 2.编码
// header('content-type:text/html;charset=utf8');
// new PDO('mysql:host=localhost;dbname=user','user','user123',
// array(PDO::MYSQL_ATTR_INIT_COMMAND=>"SET NAMES 'utf8'"));
try {
$pdo = new PDO('mysql:host=localhost;dbname=user','user','user123');
} catch (PDOException $e) {
echo '数据库链接失败'.$e->getMessage();
}
$pre = $pdo->prepare('SELECT * FROM `user`');
$exe = $pre->execute();
// var_dump($exe);
$data = $pre->fetchAll();
// print_r($data);
// $pdo->query('SET NAMES utf8');
// $pdo=null;
// echo time();
?>
<!-- // 3.实例 -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>表格</title>
</head>
<body>
<a href="add.php">添加</a>
<table border="1">
<thead>
<tr>
<th width='50px'>id</th>
<th width='100px'>姓名</th>
<th width='120px' >手机号</th>
<th width='200px' >入职时间</th>
</tr>
</thead>
<tbody>
<?php
foreach($data as $v){
?>
<tr>
<td><?= $v['id'] ?></td>
<td><?= $v['name'] ?></td>
<td><?= $v['phone'] ?></td>
<td><?= $v['add_time'] ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</body>
</html>
2.数据库添加功能
代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>添加用户</title>
<style>
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
body {
background-color: #888;
}
.add {
position: relative;
top: 50px;
left: 50px;
border-radius: 5px;
padding: 20px;
display: flex;
flex-flow: column;
justify-content: space-between;
width: 300px;
height: 180px;
gap: 5px;
background-color: lightskyblue;
box-shadow: 4px 4px 4px #666;
color: white;
align-items: center;
}
.submit {
margin-top: 15px;
background-color: lightyellow;
width: 50px;
}
h2 {
color: slateblue;
}
</style>
</head>
<body>
<form action="" class="add" method="post">
<h2>添加用户</h2>
<div>
<label for="id"> 用户ID: </label>
<input type="number" name="id" id="id" />
</div>
<div>
<label for="name">用户名:</label>
<input type="text" name="name" id="name"/>
</div>
<div>
<label for="phone">手机号: </label>
<input type="number" name="phone" id="phone" />
</div>
<input type="submit" class="submit" />
</form>
</body>
</html>
<?php
// print_r($_POST);
if(!empty($_POST)){
if(empty($_POST['id'])){
echo '<div style="color:red;margin-top:100px">id不能为空</div>';
exit;
}
if(empty($_POST['name'])){
echo '<div style="color:red;margin-top:100px">用户名不能为空</div>';
}
$pdo = new PDO('mysql:host=localhost;dbname=user','user','user123');
//查询数据
// $pre = $pdo->prepare('SELECT * FROM `user`');
// $pre->execute();
// $data = $pre->fetchAll();
// print_r($data);
//插入数据
//组装sql语句
$sql = 'INSERT INTO `user` SET `id` = ' . $_POST['id'] . ",`name` ='" .$_POST['name']. "'";
//判断手机号是否存在,如果有就添加sql语句
if(!empty($_POST['phone'])){
$sql .= ',`phone`='.$_POST['phone'];
}
$pdo->prepare($sql);
echo '<div style="color:red;margin-top:100px">'.$sql.'</div>';
//预处理
$pre = $pdo->prepare($sql);
$pre->execute();
var_dump($pre->rowCount());
if($pre->rowCount() > 0){
echo '<script>alert("添加成功");window.location.href="list.php"</script>';
}else{
echo '<script>alert("添加失败,请重试!!!")</script>';
}
}
// echo $_POST['name'];
数据库添加用户页面
添加用户成功返回列表页
sql语句