doumei4964 2018-05-05 13:31
浏览 53
已采纳

循环数组来更新mysql表

Is it possible to construct a loop which takes every single value from an array and then updates a mysql table?

What the array looks like

output

Team Blue
4
4

Team Red
4
4

Bare in mind that the 4's are Id's.

Array code

    $players = $lista;
shuffle($players); 
$player_count = count($players);
$players_per_team = $player_count/2; 
$teams = array_chunk($players,$players_per_team); 
$team_1 = $teams[0]; // Lag 1
$team_2 = $teams[1]; // Lag 2

echo "Team Blue";
echo  "<br>";
 foreach ($team_1 as $value) {
    echo $value . "<br>";
  }

  echo  "<br>";
  echo "Team Red";
  echo  "<br>";
  foreach ($team_2 as $value) {
    echo $value . "<br>";
  }

How can i select each element in the array? Basically all i need to do is to update a single column for each id in both arrays.

See picture

https://gyazo.com/ceb49db66ba85b9a6adfa0daf30c7a57 I want to update "team" column.

My guess is that i need to make some sort of loop, but i have no clue on how to select each element and then querying it.


So my question is. How can i select each element in the array and loop some sort of query to update a single column?

If i need to provide more information, just tell me! Thank you for taking your time reading my question, kind regards Jonas

  • 写回答

1条回答 默认 最新

  • dpvmjk0479 2018-05-05 13:36
    关注

    Google is your friend:

    "php mysqli update column"

    Best Result: https://www.w3schools.com/php/php_mysql_update.asp

    <?php
    $servername = "localhost";
    $username = "username";
    $password = "password";
    $dbname = "myDB";
    
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    
    $sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";
    
    if ($conn->query($sql) === TRUE) {
        echo "Record updated successfully";
    } else {
        echo "Error updating record: " . $conn->error;
    }
    
    $conn->close();
    ?>
    

    Before you update or insert data check if your value are a integer

    if (is_numeric($myArr[0])) {
        // number
    } else {
        // something other
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Excel发现不可读取的内容
  • ¥15 UE5#if WITH_EDITOR导致打包的功能不可用
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。