0

请检查我的代码,我得到以下输出:

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);

谢谢和亲切的问候,

4

2 回答 2

0

遍历匹配,将它们转义(以防万一)并将它们包装在引号和括号中;然后用逗号将它们全部连接起来,并将该字符串用作您的 VALUES 表达式:

$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";
于 2012-07-13T00:13:57.077 回答
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);
于 2012-07-13T00:27:48.747 回答