PHP查询MYSQL语句
<?php
namespace pdo_edu;
use PDO;
require 'config/connect.php';
//查询语句
$sql= 'select * from user_list';
$stmt = $pdo->prepare($sql);
$stmt->execute();
$user_lists = $stmt->fetchAll(PDO::FETCH_ASSOC);
//预处理防止SQL注入
unset($PDO);
?>
<html>
<meta charset="utf-8">
<body>
<div class="contain">
<h2>员工信息查询系统</h2>
<table border='1' cellspacing='0' cellpadding='10'>
<tr bgcolor=#ccc><th>编号</th><th>账户</th><th>性别</th><th>姓名</th><th>存款</th><th>籍贯</tr>
<?php
foreach($user_lists as $user_list){ ?>
<!-- // printf('<pre>%s</pre>',print_r($staff,true)); -->
<tr>
<td><?php echo $user_list['id'] ?></td>
<td><?php echo $user_list['user_name'] ?></td>
<td><?php echo $user_list['sex'] ?></td>
<td><?php echo $user_list['pay_name'] ?></td>
<td><?php echo $user_list['money'] ?></td>
<td><?php echo $user_list['loginaddress'] ?></td>
</tr>
<?php } ?>
</div>
</body>
</html>
PHP实现MYSQL新增数据
<?php
namespace pdo_edu;
use PDO;
require 'config/connect.php';
$user_name = $_POST['user_name'];
$sex=$_POST['sex'];
$pay_name=$_POST['pay_name'];
$money=$_POST['money'];
$loginaddress=$_POST['loginaddress'];
if(!empty($user_name)){
//查询语句
$sql= "insert `user_list` set `user_name`=? , `sex`=? , `pay_name`=? , `money`=? , `loginaddress`=? ";
// var_dump($sql);
//预处理防止SQL注入
$stmt = $pdo->prepare($sql);
$indata=[$user_name,$sex,$pay_name,$money,$loginaddress];
$stmt->execute($indata);
// PDOStatement::rowCount() 返回上一个由对应的 PDOStatement 对象执行DELETE、 INSERT、或 UPDATE 语句受影响的行数
if($stmt->rowCount() === 1 ){
// PDO::lastInsertId — 返回最后插入行的ID或序列值
// echo '新增成功'.$pdo->lastInsertId();
echo "<script>alert('success');</script>";
}else{
echo "<script>alert('field');</script>";
}
unset($PDO);
}
?>
<html>
<meta charset="utf-8">
<body>
<div class="contain">
<h2>员工信息查询系统</h2>
<form action='./insert.php' method='post' id='iset' name='iset'>
<table border='1' cellspacing='0' cellpadding='10'>
<tr bgcolor=#ccc>
<th>账户</th>
<th>性别</th>
<th>姓名</th>
<th>存款</th>
<th>籍贯</th>
</tr>
<tr>
<td>
<input type='text' name='user_name' >
</td>
<td>
<input type='text' name='sex' >
</td>
<td>
<input type='text' name='pay_name' >
</td>
<td>
<input type='text' name='money' >
</td>
<td>
<input type='text' name='loginaddress' >
</td>
</tr>
<tr>
<td rowspan='6'><input type='submit' value='提交'></td>
</tr>
</table>
</form>
</div>
</body>
</html>
PHP操作MYSQL更新字段行
<?php
namespace pdo_edu;
use PDO;
require 'config/connect.php';
$sql = 'update `user_list` set `pay_name`=? where `id`=? ';
$stmt = $pdo->prepare($sql);
$stmt->execute(['王霸',2791]);
if($stmt->rowCount()===1){
echo "更新成功";
}
PHP操作MYSQL删除字段行
<?php
namespace pdo_edu;
use PDO;
require 'config/connect.php';
$sql= 'delete from `user_list` where `id`=:id ';
$stmt= $pdo->prepare($sql);
$id=filter_input(INPUT_GET,'id',FILTER_VALIDATE_INT);
$stmt->execute(['id'=>$_GET['id']]);
if($stmt->rowCount()===1)
{
echo '删除成功';
}
unset($pdo);
总结:PHP对MYSQL表中添加数据和查询数据思路相对清晰一点。删除和修改还没有明确的认识!