How do I take out the value of foreach and update the table?

$keyyy = null;

while ($r = mysqli_fetch_array($rs)){

    $tagg = array($r["name"]);

    foreach ($tagg as $key) {
      $keyyy = $key.",";
    }
}

the printed result is

1,2,3,4,5,

just why do I only have 5, when I update to the number table?
instead of 1, 2, 3, 4, 5,

UPDATE `blog` SET
      `tags` = "{$keyyy}"
      WHERE url = "XXX"
Jul.28,2021

you may be missing a dot
$keyyy. = $key.',';


UPDATE blog SET

  `tags` = '{$keyyy}'
  WHERE url = 'XXX'
  

print the SQL statement before executing UPDATE to see if tags has changed


what is your purpose?
$keyyy = null;

while ($r = mysqli_fetch_array ($rs)) {

)
$tagg = array($r['name']);

foreach ($tagg as $key) {
  $keyyy = $key.',';
  
  echo $keyyy ;// 
}

}

the result is what you see: 1 keyyy 2, 3, 4, 5, but it actually prints this variable five times, and each time the value is overwritten by a new one, so the last value of $meme is 5,

.

if you only need to update, once, you can change it like this: $keyyy. = $key.',';
if each needs update, then put the update statement in the loop to execute.

Menu