![](/img/trans.png)
[英]PDO error when trying to insert to iSeries DB2 with php on Linux
[英]Error when trying to insert with PHP / PDO / MySQL
尝试使用两个参数进行插入,我得到了这个回报:
PDOStatement::bindValue() 期望参数 3 为 int,字符串中给出。
没有 meu banco tenho id(AUTO_INCREMENT)、nome(VARCHAR) 和 Localizaçao (VARCHAR)。 我要插入的功能是这个
public function inserir() { //create
$query = 'insert into tb_usuarios(name,localizacao)values(:name,:location:)';
$stmt = $this->conexao->prepare($query);
$stmt->bindValue('$name','$location', $this->location->__get('name','location'));
$stmt->execute();
}
我已经尝试将 Id 放入插入查询中,但没有得到任何结果,这段代码来自另一个项目,我只插入了一个字段,但表中的所有其他 3 个字段都是 Aut-increment
PDO 函数bindValue()
一次接受一个参数。 您不能在一次调用中绑定所有这些。
您尝试编写的代码应如下所示:
public function inserir() { //create
$query = 'insert into tb_usuarios(name,localizacao)values(:name,:location:)';
$stmt = $this->conexao->prepare($query);
$stmt->bindValue('name',$this->location->__get('name'));
$stmt->bindValue('location',$this->location->__get('location'));
$stmt->execute();
}
您还可以创建一个参数 name=>value 对的数组,并将该数组传递给execute()
,这与对每个参数使用bindValue()
执行相同的操作。
public function inserir() { //create
$query = 'insert into tb_usuarios(name,localizacao)values(:name,:location:)';
$stmt = $this->conexao->prepare($query);
$params = [
'name'=>$this->location->__get('name'),
'location'=>$this->location->__get('location')
];
$stmt->execute($params);
}
注意另一个区别:对于像:name
这样的占位符,您可以使用参数键':name'
或'name'
,但'$name'
不起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.