2014-06-22 90 views
0

我有一个数组。现在我想用我的数组值插入数据到我的表中。 $claw是数组php mysql从数组插入值

Array 
(
    [0] => Array 
     (
      [0] => Alabama 
      [1] => Yes 
      [2] => R 
      [3] => 

are free to enact restrictions greater than . 

      [4] => 
      [5] => 
      [6] => 
      [7] => 

It is unlawful to sell 
     ) 
) 

现在我想用这个数组值在我的表中插入数据。

我使用这个代码,但不能正常工作

public function Insert($claw) 
    { 


       $fields = array(); 
       for($i=0;$i<$claw; $i++) 
       { 
        for($j=0;$j<$claw[$i]; $j++) 
        {    
         $fields[] = $claw[$i][$j]; 
        } 
       } 

       $sql = "Insert into information values(" . implode(', ', $fields) . ')'; 

return $this->MyCommandR($sql); 


    } 

我无法理解什么我的错

+0

你需要引用你的价值观,你真的应该逃避它们。 –

+0

问题1:'$ i <$ claw' ......这看起来不正确。问题2:您的插入值需要引号。 –

+0

'$ i

回答

0

您需要定义字段来保存这些数据 $ SQL =“INSERT INTO信息(name, (“。implode(',',$ fields)。')';

$field = $conn->query("DESCRIBE `".$table."`"); 
$constrantsQueryfield = array(); 
$constrantsQueryvalue = array(); 
while($row = mysqli_fetch_assoc($field)) 
{ 

if ($row['Field']!='ID' && $row['Key']!='PRI') 
{ 
if (array_key_exists($row['Field'],$data)) 
{ 
if ($row['Key']=='DATE') 
{ 
    $constrantsQueryfield[] = $row['Field']; 
$constrantsQueryvalue[] = date("Y-m-d"); 
} 
else 
{ 
$constrantsQueryfield[] = $row['Field']; 
$constrantsQueryvalue[] = $data[$row['Field']]; 
} 
} 
} 
} 
$constrantQuery = "INSERT INTO `".$table."` "; 
for ($i = 0;$i<count($constrantsQueryfield);$i++) 
{ 
if ($i == 0){ 
$constrantQuery .= "("; 
} 
$constrantQuery .= "`".$constrantsQueryfield[$i]."`"; 
if ($i+1 == count($constrantsQueryfield)) 
{ 
$constrantQuery .= ")"; 

} 
else 
{ 
    $constrantQuery .= ", "; 

    } 
} 
$constrantQuery .= " VALUES "; 
$contstantQuery .= "(" . implode(', ', $data) . ')'; // ANy data array 

$conn->query($constrantQuery);