我是新学员。
我发现“将PHP制作的组合数据插入MySQL”有一个困难
示例:对于 1,2,3,4,组合为:
123
124
134
234
我想将其插入 MySQL 数据库。
结果:
123
124
134
234
234 <= duplicate
我找不到问题出在哪里。太感谢了。 :-)
$lista = array($a,$b,$c,$d);
$b=1;
for ($i=0; $i<=3; $i++) {
for ($j=$b; $j<=4;$j++) {
for ($k=$j+1; $j<count($lista); $j++) {
printf($lista[$i].','.$lista[$j].'<br>');
$sql="INSERT INTO table10(id)
VALUES($lista[$i]$lista[$j])";
mysql_query( $sql, $con );
}
}
$b++;
}
最佳答案
您可以创建数组以避免重复数据
$dupList=array();
//declare this array before loop
//Hold $lista[$i] and $lista[$j] jointly in a variable
$newVal=$lista[$i].$lista[$j];
if (!in_array($newVal, $dupList)) {
$sql="INSERT INTO table10(id) VALUES ($newVal)";
mysql_query( $sql, $con );
array_push($dupList,$newVal);
}
关于PHP将组合插入到mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27074838/