duandu1377 2011-11-28 18:03
浏览 57
已采纳

php pdo:prepare()否定成为正面

I'm using a prepared statement to insert values from a form into a Db, and some of the values are negative.

# html
<input type="radio" name="opinion" value='-2' />
<input type="radio" name="opinion" value='-1' />
<input type="radio" name="opinion" value='0' />
<input type="radio" name="opinion" value='1' />
<input type="radio" name="opinion" value='2' />

# prepare
prepare("UPDATE $db.$dbt SET $question = :value WHERE `key` = :key;");

Troubleshooting steps already taken:
• I echoed the POSTed values from the form and negatives return negative.
• I copied the statement in the prepare() into the CLI and manually inserted a negative value into the same column where prepare is inserting the positive value:
UPDATE db.dbt SET opinion = '-1' WHERE key = '10101'; (tickmarks present in actual SQL)
^ This worked.
• I printed the prepared statements as they're executed, and I noticed that the values are not enclosed in quotes. I think this is where the issue is coming from.

I read that values should not be quoted in the prepare(), so I can't think of anything else to do…

EDIT: The prepare() is inside a loop

foreach ( $pairs as $pair ) {
    list($question , $answer) = explode('=', $pair);
    try {
        $record_data = $dbh->prepare("UPDATE $db.$dbt SET $question = :value WHERE `key` = :key;");
        $record_data->bindParam(':value', $answer);
        $record_data->bindParam(':key', $key);
        $record_data->execute();
    } catch(PDOException $e){};
} // end foreach
  • 写回答

1条回答 默认 最新

  • douxue4395 2011-11-28 18:08
    关注

    If you are using PDO :-

    $sth->bindParam(':value', $opinion, PDO::PARAM_INT);
    

    Default data type for bindParam is PDO::PARAM_STR

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效