PHP将组合插入到mysql

标签 php mysql

我是新学员。

我发现“将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/

相关文章:

c# - 如何获取 "code first"来更新mysql中的表?

mysql - "Packet for query is too large"在带有 mysql 的 java web 应用程序中

php - 无法使用 mysqli_query 执行查询

php - 指示mod_php在脚本错误发生时发出html状态代码-可能吗?

mysql - 我们可以在 MySQL 中为多个表设置一个触发器吗?

c++ - 仅将纪元字符串保存到 mysql 日期时间年/月/日但没有小时分钟秒

android - 不使用get方法将数据从android应用程序发送到mysql

PHP Cron 作业执行时间限制失败

php - 设置 cakephp 的默认值

php - 通过 PHP 运行 CREATE TABLE 查询