Php Oracle绑定所有$u POST';s
我使用的是Php Oracle绑定所有$u POST';s,php,oracle,post,Php,Oracle,Post,我使用的是oci\u-bind\u-by\u-name() 对于所有同名的$\u POST键调用oci\u bind\u by\u name(),是否可以在PHP中动态执行此操作 为了简化我的代码,因为我有50个左右的调用来调用oci\u bind\u by\u name()只需在$\u POST数组上使用foreach循环,使用键作为参数名: // Bind all in a loop: foreach ($_POST as $key => $value) { oci_bind_by
oci\u-bind\u-by\u-name()
对于所有同名的$\u POST
键调用oci\u bind\u by\u name()
,是否可以在PHP中动态执行此操作
为了简化我的代码,因为我有50个左右的调用来调用
oci\u bind\u by\u name()
只需在$\u POST
数组上使用foreach
循环,使用键作为参数名:
// Bind all in a loop:
foreach ($_POST as $key => $value) {
oci_bind_by_name($stid, ":$key", $value);
}
但是,您不能保证客户机已经在POST中向您发送了您实际需要的密钥。然后,重要的是对照一组实际可在准备好的语句中使用的键来检查它们:
$valid_keys = array(
'post1',
'post2',
...
...
'post99'
);
然后循环这些内容,在尝试使用它们之前,检查它们是否已在邮件中发送
foreach ($valid_keys as $key) {
if (!isset($_POST[$key])) {
// ERROR! Needed key was not present in $_POST!
// Break the loop if you can't execute the statement...
}
else {
oci_bind_by_name($stid, ":$key", $_POST[$key]);
}
}
如果您打算动态地构建准备好的语句的SQL字符串,那么维护一个安全参数名称列表尤为重要。如果您确实使用一个简单的foreach循环来绑定每个变量;不要绑定到循环变量$value
// Bind all in a loop: but DO NOT use $value
foreach ($_POST as $key => $value) {
oci_bind_by_name($stid, ":$key", $_POST[$key]);
}
引用手册:
除了将
$value
更改为$\u POST[$key]
之外,这基本上是从另一个答案复制而来的。此外,我认为您应该解释为什么不应该使用$value
?我想这没什么区别。这正是我需要帮助约会的地方
// Bind all in a loop: but DO NOT use $value
foreach ($_POST as $key => $value) {
oci_bind_by_name($stid, ":$key", $_POST[$key]);
}
foreach ($ba as $key => $val) {
// oci_bind_by_name($stid, $key, $val) does not work
// because it binds each placeholder to the same location: $val
// instead use the actual location of the data: $ba[$key]
oci_bind_by_name($stid, $key, $ba[$key]);
}