简体   繁体   English

在MySQL中使用准备好的语句插入或更新

[英]Insert or Update with prepared statements in MySQL

Trying to make insert or update sql using the prepared statements from php's pdo. 尝试使用php的pdo中准备好的语句来插入或更新sql。 First I thought of using REPLACE INTO command,but it gives me an error because I have a foreign key on my index. 首先,我想到了使用REPLACE INTO命令,但这给了我一个错误,因为我的索引上有一个外键。 Read that I must use INSERT...ON DUPLICATE KEY UPDATE syntax to make it working, but it's not clear for me how to do that with prepared statements. 读到我必须使用INSERT...ON DUPLICATE KEY UPDATE语法才能使其正常工作,但我不清楚如何使用准备好的语句来执行此操作。 Any solution for this? 有什么解决办法吗? Thanks. 谢谢。

The sql is : 的SQL是:

$sql="REPLACE INTO fn_currencies(id,short,name,buy,sell,date) VALUES (:id,:short,:name,:buy,:sell,:update)";

UPD: I am making this query in Yii that uses personal wrapper over the PDO. UPD:我在Yii中进行此查询,该查询在PDO上使用个人包装。 When I use unnamed parameters I get this type of error: 当我使用未命名的参数时,会出现此类错误:

CDbCommand failed to execute the SQL statement: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens. The SQL statement executed was: INSERT INTO `fn_currencies` (id,short,name,buy,sell,date) VALUES (?,?,?,?,?,?) ON DUPLICATE KEY UPDATE id=?,short=?,name=?,buy=?,sell=?,date=? 

When I use the named parameters with differed names for Insert and Update as was mentioned..I get no errors and neither data is inserted in my DB. 如前所述,当我使用具有不同名称的命名参数进行插入和更新时。.我没有收到错误,也没有数据插入到数据库中。 Here is the schema for the DB: 这是数据库的架构:

CREATE TABLE IF NOT EXISTS `fn_currencies` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `short` varchar(4) NOT NULL,
  `name` varchar(200) NOT NULL,
  `buy` decimal(10,4) NOT NULL,
  `sell` decimal(10,4) NOT NULL,
  `date` date NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

--
ALTER TABLE `fn_currencies`
  ADD CONSTRAINT `FK_fn_currencies` FOREIGN KEY (`id`) REFERENCES `fn_operations` (`currency_id`);

Thanks to DavaRandom, he pointed out an error in my code, but this should do the trick. 感谢DavaRandom,他指出了我的代码中的错误,但这可以解决问题。 Replace the named parameter with ? 将命名参数替换为? and use an array merge to make the SQL on the fly like this: 并使用数组合并使SQL即时运行,如下所示:

$sql="
    insert INTO fn_currencies(id,short,name,buy,sell,date) 
    VALUES (?,?,?,?,?,?)
    on duplicate key update currencies set 
        short=?, name=?, buy=?, sell=?, update=?";
$values=array("id"=>1, "short"=>36, "name"=>'Bazinga', "sell"=>3.67, "date"=>'2012-08-08');
$db->query($sql, array_merge(array_values($values),array_values($values)));

Apparently this will also work (See comments all over the page about yes/no/maybe) but the above will certainly work: 显然这也可以工作(请参阅整页有关“是/否/也许”的评论),但是以上内容肯定可以工作:

$sql="
    insert INTO fn_currencies(id,short,name,buy,sell,date) 
    VALUES (:id,:short,:name,:buy,:sell,:update)
    on duplicate key update currencies set 
        short=:short, name=:name, buy=:buy, sell=:Sell, update=:update";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM