2016-05-14 41 views
0

我需要幫助完整的代碼來實現此INSERT過程: 我有空的NATAN_Procesos表,包含產品的完整表Natan_Productos ,以及存儲比較值Giro2_ID的第三個表Natan_Sesion。試圖從與匹配sesion表中的列的產品表插入匹配的行

$stmt1 = $db->query("INSERT INTO Natan_Procesos (
Producto_ID, 
Producto_Nombre, 
Producto_Descripcion, 
Proveedor_ID, 
Sucursal_ID, 
Categoria_ID, 
Giro2_ID, 
Producto_Precio, 
Producto_Descuento, 
Producto_Imagen, 
Producto_Prioridad 
) 
VALUES((SELECT Giro2_ID FROM Natan_Productos 
WHERE Giro2_ID = giro2 FROM Natan_Sesion)"); 

查詢運行後沒有任何錯誤,沒有加載到表中。 我錯過了什麼? 我應該得到的匹配Giro2_ID

,約有12的產品列表我有輸出以下查詢篩選就好了其頗爲相似:

//build the table 
echo "<table style='border: solid 1px black;'>"; 
echo "<tr><th>uid</th><th>monto</th><th>personas</th><th>ciudad</th><th>giro1</th><th>giro2</th><th>ip</th><th>Presupuesto_Individual</th></tr>"; 

class TableRows9 extends RecursiveIteratorIterator { 
    function __construct($it) { 
     parent::__construct($it, self::LEAVES_ONLY); 
    } 

    function current() { 
     return "<td style='width:150px;border:1px solid black;'>" . parent::current(). "</td>"; 
    } 

    function beginChildren() { 
     echo "<tr>"; 
    } 

    function endChildren() { 
     echo "</tr>" . "\n"; 
    } 
} 
$stmt = $db->prepare("SELECT * FROM Natan_Productos WHERE Giro2_ID = (SELECT giro2 FROM Natan_Sesion)"); 
    $stmt->execute(); 

    // set the resulting array to associative 
    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC); 
    foreach(new TableRows9(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) { 
     echo $v; 
    } 
+0

您是否意識到您正試圖對不返回任何內容的查詢執行'fetchAll'?這是INSERT查詢而不是SELECT查詢。 –

+0

我實際上已經嘗試省略這些行,但仍然沒有從$ stmt-> execute();的結果。開或關或$結果= ...打開或關閉目前沒有組合工程。因此,我需要幫助部分。我只能從INSERT和on上找到有關代碼的數據,但不能查看INSERT,UPDATE之前和之後的變量或查詢代碼。有沒有什麼幫助? –

+0

我刪除了有問題的代碼。這仍然不存儲任何地方的任何東西。 awons,你說一個不返回任何內容的查詢。請詳細說明。唯一有問題的部分是我原始文章中的第一部分代碼。 –

回答

0

答案是:

$stmt1 = $db->query("INSERT INTO db.destinationtable SELECT * FROM db.sourcetable WHERE columnname = (SELECT comparisoncolumnname FROM db.comparisontable)"); 

與PDO