到目前为止我的代码:
$query = 'INSERT INTO articles(id, name, picture, url, origAmount, currAmount, added) VALUES ';
$insertQuery = array();
$insertData = array();
$date = getDate();
foreach ($articles as $article) {
$insertQuery[] = '(?,?,?,?,?,?,?)';
$insertData[] = $article->id;
$insertData[] = $article->name;
$insertData[] = $article->picture;
$insertData[] = $article->url;
$insertData[] = $article->amount;
$insertData[] = $article->amount;
$insertData[] = $date['year'] . '-' . $date['mon'] . '-' . $date['mday'];
}
if(!empty($insertQuery)) {
$query .= implode(', ', $insertQuery);
$handle = $this->link->prepare($query);
$handle->execute($insertData);
}
如果id已经存在,我想只更新currAmount。 我试过了
$insertQuery[] = '(?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE currAmount = (currAmount)';
给出了
' SQLSTATE [42000]:语法错误或访问冲突:1064 [...]附近'(' 0192801635',' The Renaissance A Ve'在第1行'
所以行的文字内容虽然在空表上很好地插入,但内容本身并不是问题。
我该怎么做?
编辑:解决了它
在内爆之后,添加了
$query .= ' ON DUPLICATE KEY UPDATE currAmount = VALUES(currAmount)';
谢谢大家。
答案 0 :(得分:0)
... ON DUPLICATE KEY UPDATE currAmount=values(currAmount)
您忘记添加值关键字
<强>更新强> 完整的例子:
$insertQuery[] = '(?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE currAmount=values(currAmount)';
答案 1 :(得分:-1)
做这样的事情
foreach ($articles as $article) {
$insertQuery = '(?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE currAmount = (currAmount)';
$insertData = $article->id;
$insertData = $article->name;
$insertData = $article->picture;
$insertData = $article->url;
$insertData = $article->amount;
$insertData = $article->amount;
$insertData = $date['year'] . '-' . $date['mon'] . '-' . $date['mday'];
if(!empty($insertQuery)) {
$query .= implode(', ', $insertQuery);
$handle = $this->link->prepare($query);
$handle->execute($insertData);
}
}