2012-07-13 40 views
0

請檢查我的代碼,我發現了以下的輸出:PHP如何陣列數據保存到數據庫

in wood that 1 

in wood that 2 

in wood that 3 

in wood that 4 

從下面所示的代碼,我想這四行是存儲在我的MySQL數據庫。

$string = "imperfection in wood that 1 can appear during the wood drying process.imperfection in wood that 2 can appear during the wood drying process.imperfection in wood that 3 can appear during the wood drying process.imperfection in wood that 4 can appear during the wood drying process"; 
preg_match_all('/(imperfection)(.*?)(can)/', $string, $matches); 

foreach($matches[2] as $value) 
    echo $value.'<br />'; 

$sql="INSERT INTO string_check(st_name)VALUES($value)"; 
$result=mysql_query($sql); 

感謝&親切的問候,

回答

0

你要存儲這些四行成不同的行?如果是這樣的話,那麼試試這個

foreach($matches[2] as $value) { 
    $sql="INSERT INTO string_check(st_name)VALUES($value)"; 
    $result=mysql_query($sql); 
} 

下面是語法,如果你想將其存儲到一行

$values = ''; 
foreach($matches[2] as $value) { 
    $values .= $value; 
} 
$sql="INSERT INTO string_check(st_name)VALUES($values)"; 
$result=mysql_query($sql); 
+0

是的作品....謝謝魯迪...... – Reel 2012-07-13 00:32:30

0

遍歷的比賽,他們逃逸(以防萬一),並在引號和括號包裹他們;然後再加入所有的人都用逗號,並使用該字符串作爲你的價值觀表達:

$foreach($matches[2] as $match) { 
    $values[] = '("' . mysql_real_escape_string($match) . '")'; 
} 
$value_statement = implode(', ', $values); 
$sql="INSERT INTO string_check (st_name) VALUES $value_statement"; 
+0

沒有,沒有工作 – Reel 2012-07-13 00:25:33

+0

有在foreach循環中,答案相當簡單的錯誤編輯修復。 – 2012-07-13 01:14:44