傲渊山岳 发表于 2022-8-9 14:46:08

pdo 预处理

/* 通过调用驱动程序创建一个PDO实例 */
// 获取请求体,@的作用为屏蔽警告,可去除。
$post = @file_get_contents('php://input');
// 解析成数组
$post =json_decode( $post, true );
$name=$post['name'];
$age=$post['age'];
$phone=$post['phone'];

$dsn = 'mysql:dbname=mic12345;host=127.0.0.1';
$user = 'admin_read';
$password = 'matrix11';
try {

    $db = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}
$sql="INSERT INTO test(test_name,test_age,test_phone) VALUE(:test_name,:test_age,:test_phone)";
$stmt=$db->prepare($sql);
//进行参数的绑定
$stmt->bindValue(":test_name",$name);
$stmt->bindValue(":test_age",$age);
$stmt->bindValue(":test_phone",$phone);<br>   //$stmt->bindParam(":test_name",$name);   //$stmt->bindParam(":test_age",$age);   //$stmt->bindParam(":test_phone",$phone);// 执行这个sql 语句
$result=$stmt->execute();
<br>批量新增数据:
https://img2022.cnblogs.com/blog/2427663/202206/2427663-20220624164301045-2030447395.png
 
 
页: [1]
查看完整版本: pdo 预处理