2012-03-11 139 views
1

有人可以請我解釋一下如何將我輸出的數組數據導入到數據庫的行中。使用PDO將數組數據插入到數據庫中

HTML

<form id="AddRecipeForm" method="post" action="includes/add-recipe.php" class="form-inline">  
    <input type="text" name="recipe[ingredient][1]" class="input-large" placeholder="Title 1"><input type="text" name="recipe[quantity][1]" class="input-large" placeholder="Quantity 1"><br /><br /> 
    <input type="text" name="recipe[ingredient][2]" class="input-large" placeholder="Title 2"><input type="text" name="recipe[quantity][2]" class="input-large" placeholder="Quantity 2"><br /><br /> 
    <input type="text" name="recipe[ingredient][3]" class="input-large" placeholder="Title 3"><input type="text" name="recipe[quantity][3]" class="input-large" placeholder="Quantity 3"><br /><br /> 
    <button type="submit" class="btn">Add Recipe</button> 
</form> 

這被傳遞到一個PHP形式:

foreach($_POST['recipe'] as $key=>$value) 
    { 

    } 

print_r($_POST); 

和輸出以下的數組:

Array ( 
    [recipe] => Array ( 
     [ingredient] => Array ( 
      [1] => eggs 
      [2] => milk 
      [3] => flour 
     ) [quantity] => Array ( 
      [1] => 12 
      [2] => 13 
      [3] => 14 
     ) 
    ) 
) 

我需要將每個單獨的成分和數量導入數據庫表中的新行。我正在使用PDO連接到我的數據庫,但我不確定如何將數據中的數據插入到數據庫的行中。

謝謝。

+0

重組您的數據庫,並有一個單獨的配料表......不要將它們全部存儲在配方表中的列中 – 2012-03-11 20:50:33

+0

@Mark Ba​​ker感謝您的評論。我目前有我的數據庫分成成分,食譜標題和食譜項目表。我需要從數組中獲取數據並將其插入到食譜項目表中,但我不確定如何使用PDO來做到這一點。任何幫助將非常感激。 – 2012-03-11 20:57:32

回答

2

嗯,我會不同stucture我的形式有點讓你獲得assemebled作爲各自爲陣ingerdients,但與stcuture您有:

$db = new PDO($dsn, $user, $pass); 

$stmt = $db->prepare('INSERT INTO ingredient (name, quantity) VALUES (?,?)'); 

// youll want to verify that both arrays have the same number of elements before doing this 
// as part of your validation 
$ingredients = array_combine($_POST['recipe']['ingredient'], $_POST['recipe']['quantity']); 

$errors = array(); 

foreach($ingredients as $name => $quantity) 
{ 
    try { 
     $stmt->execute(array($name, $quantity)); 
    } catch (PDOException $e) { 
     $errors[] = array(
      'message' => "Could not insert \"$name\", \"$quantity\".", 
      'error' => $e 
    }  
} 

if(!empty($errors)) { 
    //do something? 
} 
+0

感謝大家的帖子@prodigitalson上面的代碼做的伎倆。非常感謝! – 2012-03-11 21:13:51

0

我想你的問題是$_POST陣列的格式。您有:

[recipe][ingredient][...] and 
[recipe][quantity][...] 

但是那不是你的數據庫,以行和列組織它的結構:

[recipe][...][ingredient, quantity] 

你可以看到周圍怎麼[...]移動。您需要將的數組格式轉換爲您的數據庫格式。這是最簡單的一個foreach完成:

$recipes = array(); # Zero rows to insert we start with. 
$formRecipes = $_POST['recipe']; # The form recipes are located here. 

# The [...] part is in ingredient: 
foreach ($formRecipes['ingredient'] as $index => $ingredient) 
{ 
    # the [...] part is $index now 
    $recipes[$index]['ingredient'] = $ingredient;  
    $recipes[$index]['quantity'] = $formRecipes['quantity'][$index]; 
} 

您已經運行在此之後,使用print_r來驗證一切都正確:

print_r($recipes); 

您現在可以使用$recipes陣列將數據插入到數據庫每行(我假設你知道你如何做插入SQL查詢,所以我不把它放入答案)。

1

簡單的例子沒有錯誤檢查:

<?php 

    $dbc = new PDO(/* ... */); 

    $stmt = $dbc->prepare("INSERT INTO tbl(ingredient,quantity) VALUES(:ingredient,:quantity);"); 
    $numIngredients = count($_POST['recipe']['ingredient']); 
    for ($i=1; $i <= $numIngredients; $i++) { 
     $stmt->execute(array(
      ':ingredient' => $_POST['recipe']['ingredient'][$i], 
      ':quantity' => $_POST['recipe']['quantity'][$i] 
     )); 
    } 

?> 

需要注意的是,通常你應該開始計數爲0的索引,如果你只是寫recipe[ingredient][] PHP會自動創建索引。

相關問題