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

循环用于使用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
    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

    点赞 评论

相关推荐