-1

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

4
  • Possible duplicate of update mysql db with php loop from array Commented May 5, 2018 at 13:33
  • Since you are already looping through each array, just add a db update query inside each loop. Commented May 5, 2018 at 13:38
  • @Sean you answered my question, thanks! You could "answer question" and i'll mark it as the answer Commented May 5, 2018 at 13:42
  • @pradeep i don't think so. Commented May 5, 2018 at 13:42

1 Answer 1

2

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
}
Sign up to request clarification or add additional context in comments.

3 Comments

Read my question! Got no problems with making the query.
you already have your loops within the foreach $value is your id or not?
I got it working, im querying inside each foreach loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.