Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP将数组值插入MySQL数据库_Php_Mysql - Fatal编程技术网

使用PHP将数组值插入MySQL数据库

使用PHP将数组值插入MySQL数据库,php,mysql,Php,Mysql,我在将数组值插入db表时遇到问题…请帮助我解决此问题…提前感谢 $array1=([0]=>val1,[1]=>val2,[2]=>val3); $array2=([0]=>val1,[1]=>val2,[2]=>val3); $array3=([0]=>val1,[1]=>val2,[2]=>val3); INSERT INTO table1 (id,date,col1,col2,col3,col4) VALUES (1,now(),'$

我在将数组值插入db表时遇到问题…请帮助我解决此问题…提前感谢

$array1=([0]=>val1,[1]=>val2,[2]=>val3);
$array2=([0]=>val1,[1]=>val2,[2]=>val3);
$array3=([0]=>val1,[1]=>val2,[2]=>val3);

INSERT INTO table1 (id,date,col1,col2,col3,col4) VALUES (1,now(),'$array1','$array2','$array3',50); 
在表1(id、日期、col1、col2、col3、col4)中插入值(1,now(),“$array1[0]”,“$array1[1]”,“$array1[2]”,50)


括号([])表示要插入的数组位置。

如果要插入一行,请使用
内爆()
类似

$arr1 = implode(',', $array1)
$arr2 = implode(',', $array2)
$arr3 = implode(',', $array3)

$sql = "INSERT INTO table1 (id,date,col1,col2,col3,col4) VALUES (1,now(),'$arr1','$arr2','$arr3',50)"; 
否则您需要使用
for()
foreach()

对于($i=0;$i您可以这样尝试,(这只是一个示例)


使用循环。您希望字段存储整个数组还是每个元素只存储一行?如果您希望存储数组,可以将元素内爆为字符串,以后需要读取时,使用explode。我希望每个元素添加一行……您不能将数组直接插入表中。您需要使用foreach来迭代数组中的所有元素e arrayi使用了这种格式,因为循环不起作用……它在db表中添加了“A,r”
for($i =0; $i <=count($array1); $i++){
  $query="INSERT INTO 
  table1 (id,date,col1,col2,col3,col4)
  VALUE
  (1,now(),".$row[$i].",".$row[$i].",".$row[$i].",50)";
}
foreach($array as $row){
  $query="INSERT INTO 
  table1 (id,date,col1,col2,col3,col4)
  VALUE
  (1,now(),".$row['yourfieldname'].",".$row['yourfieldname'].",".$row['yourfieldname'].",50)";
}