[英]php mysql insert value from an array
I have an array.Now I want to insert data in my table using this array value. 我有一个数组。现在我想使用此数组值在表中插入数据。 $claw
is the array $claw
是数组
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
)
)
Now I want to insert data in my table using this array value. 现在,我想使用此数组值在表中插入数据。
I am using this code but not work 我正在使用此代码,但无法正常工作
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);
}
I cant understand whats my wrong 我不明白我怎么了
You need to define the field to save these data $sql = "Insert into information ( name
, yes
, ABC
, CBA
) values(" . implode(', ', $fields) . ')'; 您需要定义字段以保存这些数据。$ sql =“插入信息( name
, yes
, ABC
, CBA
)values(”。implode(',',$ fields)。')';
or 要么
$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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.