2015-05-25 75 views
-1

我希望將mass_reach列中的數據庫中的當前值添加到變量名稱$ newmass(常數值)下的新值中。我在下面創建了一個雙桶循環來提取當前的值,然後添加它並更新第二個循環中的值,但似乎無法實現。添加到已經在mySQL數據庫中的東西

if(isset($_POST['mass_reach'])){ 
    $massreach = $_POST['mass_reach']; 

     foreach ($massreach as $massreach_id) { 
     $query4 = "SELECT mass_reach FROM coaches WHERE id='{$massreach_id}'";  
     $result4 = mysqli_query($con, $query4); 
     $query2 = "UPDATE coaches SET mass_reach='{$newmass}'+'{$result4}' WHERE id='{$massreach_id}'";   
     $result = mysqli_query($con, $query2); 
     } 
} 

I am getting the error: I get the error: Catchable fatal error: Object of class mysqli_result could not be converted to string in line 8 ie

$query2 = "UPDATE coaches SET mass_reach='{$mass}'+'{$result4}' WHERE id='{$massreach_id}'";   
+0

您還需要在更新之前獲取數據。 –

+0

提取對象怎麼樣? –

+0

更新了$ result4。不知道該對象將如何結構化。 Uchiha – Welsh

回答

0
function get_string_between($string, $start, $end){ 
$string = " ".$string; 
$ini = strpos($string,$start); 
if ($ini == 0) return ""; 
$ini += strlen($start);  
$len = strpos($string,$end,$ini) - $ini; 
return substr($string,$ini,$len);} 

foreach ($mass_reach as $string) { 

    $c_id = get_string_between($string,"[","]"); 
    $c_mass = get_string_between($string,"(",")"); 
    $query2 = "UPDATE coaches SET mass_reach='{$mass}' + '{$c_mass}' WHERE id='{$c_id}'";  
    $result2 = mysqli_query($con, $query2); 
} 
+0

我設法寧願將數據庫中的數據添加到數組中,然後進行更新。謝謝你們的幫助。 – Welsh

相關問題