PHP的PDO的bindParam与bindValue? [英] PHP PDO with bindParam vs bindValue?

查看:86
本文介绍了PHP的PDO的bindParam与bindValue?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决方案

更改此:

foreach($fields as $dbfield => $field) {
    $value = isset($_POST[$field]) ? 1 : 0;
    $STH -> bindParam( ':' . $dbfield, $value, PDO::PARAM_INT, 1 );
}

对此:

foreach($fields as $dbfield => $field) {
    $value = isset($_POST[$field]) ? 1 : 0;
    $STH -> bindValue( ':' . $dbfield, $value, PDO::PARAM_INT );
}


问题

我有以下代码,该代码不会出现任何错误,但不能满足我的要求:

I have the following code, which doesn't give any errors, but it doesn't do what I need it to do:

PHP

$fields = array(
    'db_col_1'  => 'cb_1',
    'db_col_2'  => 'cb_2',
    'db_col_3'  => 'cb_3'
);

$parts = array();

foreach($fields as $dbfield => $field){
    $parts[] = '`' . $dbfield . '` = :' . $dbfield;
}

$DBH = new PDO( "mysql:host=localhost;dbname=db", "user", "pass" );
$DBH -> setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

//temp id
$id = 1;

$STH = $DBH -> prepare( 'UPDATE `table1` SET ' . join(', ', $parts) . ' WHERE `id`= :id' );

$STH -> bindParam( ':id', $id, PDO::PARAM_INT, 10 );

foreach($fields as $dbfield => $field) {
    $value = isset($_POST[$field]) ? 1 : 0;
    $STH -> bindParam( ':' . $dbfield, $value, PDO::PARAM_INT, 1 );
}

$STH -> execute();

HTML

<input id="cb_1" name="cb_1" type="checkbox" value="cb_1" />
<br />
<input id="cb_2" name="cb_2" type="checkbox" value="cb_2" />
<br />
<input id="cb_3" name="cb_3" type="checkbox" value="cb_3" />

数据库将得到更新,但是如果我按如下所示选中复选框:

The database goes get updated, but if I check the checkboxes as follows:

示例复选框状态

cb_1 = 1
cb_2 = 1
cb_3 = 0

数据库中的每一列都获得0.

Every column in the db gets 0.

如果我选中复选框,如下所示:

If I check the checkboxes as follows:

示例复选框状态

cb_1 = 0
cb_2 = 0
cb_3 = 1

数据库中的每一列都获取1.

Every column in the db gets 1.

有人知道如何解决这个问题吗?

Anyone know how to fix this?

是因为bindParam绑定了实际变量$value(通过引用),我认为在循环结束时,该变量具有基于$_POST['cb_3']的值?

Could it be because bindParam binds the actual variable $value (by reference) which I think, by the time the loop ends, has a value based on $_POST['cb_3']?

所以我认为我应该使用bindValue?但不确定如何.我检查了文档,但感到困惑.

So I think I am supposed to be using bindValue? But not sure how.. I've checked the documentation, but find it confusing.

推荐答案

PDOStatement :: bindParam ()是一种告诉PDO的方法:稍后执行此语句时,请从该变量中读取值".这意味着在调用$STH -> execute()时,$value必须存在并且必须保留其原始值.

PDOStatement::bindParam() is a way to tell PDO: «when you execute this statement later, please read the value from this variable». That means that when call $STH -> execute() the $value must exist and must keep its original value.

由于您是通过这种方式使用的:

Since you are using it this way:

foreach($fields as $dbfield => $field) {
    $value = isset($_POST[$field]) ? 1 : 0;
    $STH -> bindParam( ':' . $dbfield, $value, PDO::PARAM_INT, 1 );
}

... $value在每次循环迭代中都会被覆盖,并且您的值会丢失. (仅剩下最后一个.)

... $value gets overwritten on each loop iteration and your values get lost. (Only the last one remains.)

您需要使用 PDOStatement :: bindValue()表示:«存储该值,并在以后执行语句»时使用它.这样,就不再需要该变量.

You need to use PDOStatement::bindValue() that means: «store this value and use it later when you execute the statement». This way, the variable is no longer necessary.

这篇关于PHP的PDO的bindParam与bindValue?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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