I
I
Ibishka2020-05-14 20:35:46
PHP
Ibishka, 2020-05-14 20:35:46

Is it bad to output html like this?

<?php
      if (isset($_POST['register'])) {
        $user = mysqli_fetch_assoc(mysqli_query($connection, "SELECT * FROM `users` WHERE `email` = '" . $_POST['email'] . "'"));
        if ($user) {
        echo  '<p class="error">This email address is already associated with an account.</p>';
        }
      }
      ?>

Or is that better?
<?php
      if (isset($_POST['register'])) {
        $user = mysqli_fetch_assoc(mysqli_query($connection, "SELECT * FROM `users` WHERE `email` = '" . $_POST['email'] . "'"));
        if ($user) {
      ?>
          <p class="error">This email address is already associated with an account.</p>
      <?php
        }
      }
      ?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FanatPHP, 2020-05-14
@Ibishka

Both options are bad.
It implies that the output goes directly mixed with queries to the database. And you must first execute all queries, collect all the data for output, and only then start outputting.

<?php
if (isset($_POST['register'])) {
    if ($user) {
        $error = "This email address is already associated with an account.";
    }
  // остальные проверки

and only after we have finished all the checks, we begin to display html.
But what is really bad in these pieces of code is that the variable is shoved directly into the request.
Yes. And there is no "overhead" when using echo, especially in comparison with other output options, in nature - these are all grandmother's tales.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question