2012-07-13 37 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