使用PDO创建长插入SQL查询的最佳方法
Considering I'm building a form and posting the data to PHP, a typical insert query for me goes like this:
考虑到我正在构建表单并将数据发布到PHP,对我来说典型的插入查询是这样的:
$sql = "INSERT INTO news
(title, body)
VALUES (?, ?)";
$stmt = $db->prepare($sql);
$stmt->execute(array($_POST["title"], $_POST["body"]));
$stmt->closeCursor();
This looks fine for a small query, and it is my understanding that this keeps me safe from the likes of SQL injection and whatnot.
对于一个小查询来说这看起来很好,而且我的理解是这使我免受SQL注入和诸如此类的攻击。
But what if I need to work with a pretty big form? Something like...
但是,如果我需要使用相当大的形式呢?就像是...
$sql = "INSERT INTO ficha_item
(titulo, tipo_produto, quantidade_peso, unidade_de_venda,
unidades_por_caixa, caixas_piso, pisos_palete, tipo_de_palete,
unidades_palete, caixas_palete, uni_diametro, uni_largura,
uni_profundidade, uni_altura, uni_peso_bruto_unidade, caixa_largura,
caixa_profundidade, caixa_altura, altura_palete, volume_unidade,
peso_caixa, peso_palete)
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
I think it becomes tedious counting all these question marks. Is there a cleaner way to do this with PDO?
我认为计算所有这些问号变得乏味。使用PDO有更清洁的方法吗?
3 个解决方案
#1
6
I would do something like this:
我会做这样的事情:
$fields = array(
'titulo',
'tipo_produto',
'quantidade_peso',
'unidade_de_venda',
'unidades_por_caixa',
'caixas_piso',
'pisos_palete',
'tipo_de_palete',
'unidades_palete',
'caixas_palete',
'uni_diametro',
'uni_largura',
'uni_profundidade',
'uni_altura',
'uni_peso_bruto_unidade',
'caixa_largura',
'caixa_profundidade',
'caixa_altura',
'altura_palete',
'volume_unidade',
'peso_caixa',
'peso_palete'
);
$sql = 'INSERT INTO ficha_item ( %s ) VALUES ( %s )';
// make a list of field names: titulo, tipo_produto /*, etc. */
$fieldsClause = implode( ', ', $fields );
// make a list of named parameters: :titulo, :tipo_produto /*, etc. */
$valuesClause = implode( ', ', array_map( function( $value ) { return ':' . $value; }, $fields ) );
// or, with create_function
$valuesClause = implode( ', ', array_map( create_function( '$value', 'return ":" . $value;' ), $fields ) );
$sql = sprintf( $sql, $fieldsClause, $valuesClause );
// $sql is now something similar to (formatted for display):
// INSERT INTO ficha_item
// ( titulo, tipo_produto /*, etc. */ )
// VALUES
// ( :titulo, :tipo_produto /*, etc. */ )
$stmt = $db->prepare($sql);
// if the keys in $_POST match with $fields, you can now simply pass $_POST here
$stmt->execute( $_POST );
// or, as per Bill Karwin's sound suggestion, with the intersection of $_POST
$stmt->execute( array_intersect_key( $_POST, array_flip( $fields ) ) )
In other words, use named parameters, and have them be dynamically generated based on the $fields
array. Although named parameters are not strictly necessary; they do help make things easier, because the order of elements passed to, for instance, PDOStatement::execute()
doesn't matter now anymore.
换句话说,使用命名参数,并根据$ fields数组动态生成它们。虽然命名参数不是绝对必要的;它们确实有助于简化操作,因为传递给PDOStatement :: execute()的元素顺序现在不再重要了。
This assumes though, that the number of elements and their keys in $_POST
, exactly match the number of fields and their names in $sql
.
但是,假设$ _POST中的元素及其键的数量与$ sql中的字段数及其名称完全匹配。
更多相关文章
- ORACLE PL/SQL编程详解之二:PL/SQL块结构和组成元素(为山九仞,岂一
- Android 5.X Activity过渡动画,以及漂亮的共享元素效果
- GridView中实现元素填充剩余空间(自适应)
- android-exploitme(四):参数篡改
- 如何在函数中将两个参数从1个类传递给另一个?
- 将XML元素反序列化为Java Map
- Android的startActivityForResult()与onActivityResult()与setRe
- Android开发入门之为应用添加多个Activity与参数传递
- 按下子活动后退按钮后,Android主要活动的元素不响应