Php可重复区域

So i got this code, at the moment it is repeating everything , and i just wanted it to repeat the echo, so i get all usernames from it, if i leave it as it is it will also repeat the form when i press a username. Every time i tried to ajust it, it just gave me syntax errors

<?php do { ?>
        <?php
                 $username = $row_mensagens['username'];
                 $user     = $row_mensagens['id'];

        if(isset($_GET['user']) && !empty($_GET['user'])){
          ?>

          <form>
            Introduz mensagem : <br>
            <textarea name='message' rows='7' cols='60'></textarea>
            <br><br>
            <input type='submit' value="Send Message" />
          </form>

          <?php
        } else {

                  echo "<p><a href='mensagens.php?user=$user'>$username</a></p>";


              }


        ?>
          <?php } while ($row_mensagens = mysql_fetch_assoc($mensagens)); ?>

that do { } while() will always repeat as many as the number of records come from database.

You can do it this way:

<?php
    if(isset($_GET['user']) && !empty($_GET['user'])){
?>
<form>
    <input type="hidden" name="user" value="<?php echo $_GET['user']; ?>" /> <!-- hidden field so you can process to who -->
    Introduz mensagem : <br>
    <textarea name='message' rows='7' cols='60'></textarea>
    <br>
    <br>
    <input type='submit' value="Send Message" />
</form>

<?php
    } else {
        do {
            $username = $row_mensagens['username'];
            $user     = $row_mensagens['id'];
            echo "<p><a href='mensagens.php?user=$user'>$username</a></p>";
        } while ($row_mensagens = mysql_fetch_assoc($mensagens));
    }
?>

Move do { inside else and show the form only if you have a $_GET['user']

I have also added for you a hidden field, so you know who to send message.

Hope you understand how this works. Documentation on Control Structures: do-while

I also suggest to make that form a post form, as by default it is a get form, and since you have a textarea you are more likely to bump into errors if the message is too long.

LE: Another suggestion, try to move to PDO or mysqli_* functions since mysql_* functions are considered deprecated as of PHP 5.5 and have some good chances to be removed.