0

I have this query where it copies the existing rows: I need to set another column themeid as $new_theme_id value. How do I add that to this query?

$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; }

This query doesn't work.

My table structure:

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

Please help!

Alex G
  • 3,048
  • 10
  • 39
  • 78
  • try like this: `"INSERT INTO theme_styles (SELECT selector, property, value FROM theme_styles WHERE themeid=?), $new_theme_id";` ? – jogesh_pi Mar 02 '15 at 04:54
  • What exactly is the problem with the current code? Does it give the wrong results? Throw an exception? – Mureinik Mar 02 '15 at 04:56
  • @jogesh_pi: doesn't work, i even tried: `$query = "INSERT INTO theme_styles (SELECT selector, property, value FROM theme_styles WHERE themeid=?), '$new_theme_id' AS themeid";`. No error messages are thrown.. – Alex G Mar 02 '15 at 05:03
  • @AlexG can you echo the `$query` here ? – jogesh_pi Mar 02 '15 at 05:06
  • @jogesh_pi: `INSERT INTO theme_styles (SELECT selector, property, value FROM theme_styles WHERE themeid=?), 20 AS themeid`. Caught an error: `#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?), 20 AS themeid' at line 1 ` – Alex G Mar 02 '15 at 05:07

1 Answers1

1

well, I think you need to make sure that your column matches your query exactly, however, you have 5 column in your new table but only 4 column in the query. try this $query = "INSERT INTO `theme_styles` (`selector`, `property`, `value`, `themeid`) SELECT `selector`, `property`, `value`, '$new_theme_id' AS themeid FROM `theme_styles` WHERE themeid=?";

jhdxr
  • 166
  • 1
  • 4