2015-03-02 27 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