PHP + PDO:如果参数为空则绑定 null [英] PHP + PDO: Bind null if param is empty

查看:69
本文介绍了PHP + PDO:如果参数为空则绑定 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试这个(并且在用户发送之前处理所有 PoST var,没有 SQL 注入问题):

I'm trying this (and all PoST var are treated before user send it, no SQL Injection worries):

$stmt = $con->prepare($sql);
$stmt->bindParam(":1", $this->getPes_cdpessoa());
$stmt->bindParam(":2", $this->getPdf_nupessoa_def());

当这些变量中的任何一个为 NULL 时,PDO 会哭泣并且不让执行我的语句,并且在我的表上,我确实允许这些字段可以为空.

When any of those vars are NULL, PDO cries and don't let execute my statement, and on my Table, i DO allow these fields beign nullables.

有什么方法可以检查值是否为空,pdo 只是将 NULL 绑定到 then(我的意思是,一个聪明的方法是 if(empty($_POST['blablabla')...) 每个参数?

Is there any way to check if the values are empty, pdo just bind NULL to then (and i mean, a smart way instead if(empty($_POST['blablabla')...) for every single param?

推荐答案

尝试:

$stmt = $con->prepare($sql);
$stmt->bindParam(':1', $this->getPes_cdpessoa(), PDO::PARAM_NULL);
$stmt->bindParam(":2", $this->getPdf_nupessoa_def(), PDO::PARAM_NULL);

另见:

这篇关于PHP + PDO:如果参数为空则绑定 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆