Simply concatenate a comma onto the id, each time you add to the $tmp
variable.
$stmt = $con->query($sql);
$tmp = '';
foreach ($stmt as $row){
$tmp .= $row['user_id'] . ',';
}
$tmp = trim($tmp, ','); // remove trailing comma
echo $tmp;
In answer to your comment:
$v=trim($tmp, ',');
$insert = $con->prepare("INSERT INTO $dtBe
(user_mobile,user_id,posting_date)
VALUES ('$umobile',$v,'$posting_date')");
$insert->execute();
But there is no benefit or security in preparing a query when you have already concatentated values into.
So use a proper prepared and bound approach.
$v=trim($tmp, ',');
$insert = $con->prepare("INSERT INTO $dtBe
(user_mobile,user_id,posting_date)
VALUES (?,?,?)");
$insert->bind_param('sss', $umobile, $v,$posting_date);
$insert->execute();