ds3422222 2018-11-27 19:49
浏览 89
已采纳

循环用于使用php在mysql数据库中插入行

I have a for loop with php but is not working properly. Inside the for loop I have an "if" and an "else", but the loop stops iterate in the first "else" and should continue. Here is the code:

//counting the rows in database and the rows I want to insert

$total = count($rowToInsert); //for example 10 values
$totalDB = count($rowDB); // for example 5 values

// the for loop starts

for ($i=0; $i < $total; $i++){ //it should iterate until 10 values

  if(isset($rowDB[$i])){ //change the first 5 values

   $update = "UPDATE table SET name = '$name[$i]' WHERE ID = $rowToInsert[$i]";
   $result = mysqli_query($con, $update);

  } else { //it should iterate from sixth until tenth value

   $insert = "INSERT INTO table (name) VALUES ('$name[$i]')";
   $result = mysqli_query($con, $insert);

  // here is the next code

  $newTable = 'table'.$rowToInsert[$i];

  $newDB = "CREATE DATABASE $newTable CHARACTER SET utf8 COLLATE utf8_general_ci";
  $resultDB = mysqli_query($con, $newDB);

  // select the DB

  mysqli_select_db($con, $newTable) or die ("not found");

  } //end of else

} //end of for

The thing is if the database contain 5 rows and I want to insert, for example, 10 rows, the code works updating the first 5 with the new value, then it jumps to the "else" and starts to iterate in the sixth value and it works, but the next values doesn't.

Any idea what i'm doing wrong? Thanks!

Hector

  • 写回答

1条回答 默认 最新

  • dream890110 2018-11-27 23:22
    关注

    Ok I found the problem. In the else loop, when the iteration tries to select the database, for some reason it takes part of the name of the last iteration, so it couldn't find the database. The solution (maybe is not so clean) is connect and close the database connection in every iteration. The code stays like this:

    //counting the rows in database and the rows I want to insert
    
    $total = count($rowToInsert); //for example 10 values
    $totalDB = count($rowDB); // for example 5 values
    
    // the for loop starts
    
    for ($i=0; $i < $total; $i++){ //it should iterate until 10 values
    
      if(isset($rowDB[$i])){ //change the first 5 values
    
       $update = "UPDATE table SET name = '$name[$i]' WHERE ID = $rowToInsert[$i]";
       $result = mysqli_query($con, $update);
    
      } else { //it should iterate from sixth until tenth value
    
       // reconnect to db
    
       $con = mysqli_connect($host, $user, $pass) or die ("unable to connect");
       $db = "database";
    
       $insert = "INSERT INTO table (name) VALUES ('$name[$i]')";
       $result = mysqli_query($con, $insert);
    
      // here is the next code
    
      $newTable = 'table'.$rowToInsert[$i];
    
      $newDB = "CREATE DATABASE $newTable CHARACTER SET utf8 COLLATE utf8_general_ci";
      $resultDB = mysqli_query($con, $newDB);
    
      // select the DB
    
      mysqli_select_db($con, $newTable) or die ("not found");
    
      //close the connection to db;
    
      $con->close();
    
      } //end of else
    
    } //end of for
    

    Thanks you all for inspiring the answer!

    Hector

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有偿求qftp工具。能连接,下载文件,发送代码,windows环境,最好qt6 要qt creator写的
  • ¥70 刚刚看到一个人的网站居然是通过cname访问的
  • ¥15 Attributeerror:super object has no attribute '__sklearn_tags__'_'
  • ¥15 逆置单链表输出不完整
  • ¥15 宇视vms-B200-A16@R启动不了,如下图所示,在软件工具搜不到,如何解决?(操作系统-linux)
  • ¥500 寻找一名电子工程师完成pcb主板设计(拒绝AI生成式答案)
  • ¥15 关于#mysql#的问题:UNION ALL(相关搜索:sql语句)
  • ¥15 matlab二位可视化能否针对不同数值范围分开分级?
  • ¥15 已经创建了模拟器但是不能用来运行app 怎么办😭自己搞两天了
  • ¥15 关于#极限编程#的问题,请各位专家解答!