2017-06-19 46 views
0

我需要一個幫助。我需要使用PHP和MySQL將數組數據更新爲不同列的單個記錄。我在下面解釋我的表格和代碼。如何使用PHP和MySQL更新單個記錄中的數據數組

cn_sell_info:

sid  name file1 file2 file3 file4 

1  Raju 

在這裏,我需要更新四個文件中的字段。我在下面解釋我的數據陣列。

$result= 
Array 
(
[0] => Array 
    (
     [img] => abc.png 
     [status] => 1 
    ) 

[1] => Array 
    (
     [img] => sdfg.png 
     [status] => 1 
    ) 

[2] => Array 
    (
     [img] => 
     [status] => 0 
    ) 

[3] => Array 
    (
     [img] => 1sqd.png 
     [status] => 1 
    ) 

) 

在這裏,我需要其狀態爲1,相應的圖像將在文件列串行更新。

請幫忙。

回答

4

嘗試此,

$result=array(array("img"=>"abc.png","status"=>1),array("img"=>"sdfg.png","status"=>1),array("img"=>"erty.png","status"=>0),array("img"=>"1sqd.png","status"=>1)); 
$concate_sql = array(); 
$inc_ = 0; 
foreach($result as $key=>$v){ 
    $inc_++; 
    $img = $v['img']; 
    $status = $v['status']; 
    if($status==1){ 
     $concate_sql[] = " `file".$inc_."`='".$img."' "; 
    } 
} 
$added_sql = count($concate_sql)?implode(",",$concate_sql):''; 
$update = "update table_name set ".$added_sql." where `sid`='1';"; 
echo $update; 

[https://www.jdoodle.com/embed/v0/php/5.6.16/3RV]

輸出。

update table_name set `file1`='abc.png' , `file2`='sdfg.png' , `file4`='1sqd.png' where `sid`='1'; 
+0

我不想更新僅名稱文件字段。 – subhra

+0

因此,從查詢@subhra中刪除名稱。 –

+0

你可以更新你的代碼,以便我可以upvote你嗎? – subhra

相關問題