2014-06-22 131 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);