2014-03-04 28 views
0

這是我的var_dump($ img);將數組插入到mysql的多個行中

array(2) { 
    [0]=> 
    string(5) "1.jpg" 
    [1]=> 
    string(5) "2.PNG" 
} 

在開始我想我可以使用序列化,但它只能插入一行。下面

foreach($img as $k){ 

//Insert into database here. This will add all the records which are in array. 

} 

if(!empty($img_src)){ 

    foreach ($img_src as $key => $value) { 
    $targeted_post_id = $db->insert_id; 

    $stmt = $db->prepare("INSERT INTO photo_upload(`post_id`,`img_src`) VALUES (?,?)"); 
    $stmt->bind_param('is', $targeted_post_id,$value); 
    $stmt->execute(); 

    } 

} 
+0

使用foreach($ img); –

+2

並在foreach中寫入插入查詢 – SajithNair

回答

0

使用代碼將價值1.png在每個循環& 2.png

+0

無法使用prepare語句?我收到了一個行,雖然我有兩個項目 – user3346088

+0

你可以提供你的代碼的問題? –

+0

我更新了我的答案 – user3346088

0

使用此方法在數據庫中逐個插入數組值。

使用foreachfor循環語句

例如。

foreach($img as $key=>$image) { 
     echo $image; 
     //Insert query statement 
    } 

對於循環語句。

$image = count($img); 
    for($i=0;$i<=$image;$i++) { 
     echo $img[$i]; 
     //Insert query statement 
    } 

你應該插入單列意味着你可以使用serialize()功能。

+0

我使用pdo和循環我仍然插入1行的結果。 $ stmt-> bind_param('is',$ post_id,$ image); – user3346088

+0

請看看我的代碼 – user3346088