为准备好的语句设置一个额外的列值

时间:2015-03-02 04:36:06

标签: php mysql

我有这个查询,它复制现有的行: 我需要将另一列themeid设置为$new_theme_id值。如何将其添加到此查询?

$existing_theme_id = 1234;
$new_theme_id = 5678;
$query = "INSERT INTO theme_styles SELECT selector, property, value, '$new_theme_id' AS themeid FROM theme_styles WHERE themeid=?";
try { $stmt = $dbh->prepare($query); $stmt->execute(array($theme_id)); } catch(PDOException $ex) { echo 'Query failed: ' . $e->getMessage(); exit; }

此查询不起作用。

我的表结构:

id      int(6)  AUTO_INCREMENT  
themeid     int(4)
selector    varchar(100)    latin1_swedish_ci
property    varchar(50) latin1_swedish_ci
value       mediumtext  latin1_swedish_ci

请帮忙!

1 个答案:

答案 0 :(得分:1)

好吧,我认为您需要确保您的列与您的查询完全匹配,但是,您的新表中有5列,但查询中只有4列。 试试这个 $query = "INSERT INTO `theme_styles` (`selector`, `property`, `value`, `themeid`) SELECT `selector`, `property`, `value`, '$new_theme_id' AS themeid FROM `theme_styles` WHERE themeid=?";