2015-03-02 67 views
0

我有这个查询它复制现有的行: 我需要设置另一列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 

请帮帮忙!

+0

尝试这样的:' “INSERT INTO theme_styles(SELECT选择,属性值从theme_styles WHERE的ThemeID =?),$ new_theme_id”;'? – 2015-03-02 04:54:14

+0

现在的代码究竟是什么问题?它会给出错误的结果吗?抛出异常? – Mureinik 2015-03-02 04:56:48

+0

@jogesh_pi:不起作用,我甚至试过:'$ query =“INSERT INTO theme_styles(SELECT selector,property,value FROM theme_styles WHERE themeid =?),'$ new_theme_id'AS themeid”;'。没有错误消息被抛出.. – 2015-03-02 05:03:05

回答

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=?";

+0

这工作。谢谢。 – 2015-03-03 01:23:27