Error: Parse error: syntax error, Unexpected '}'

Asked

Viewed 346 times

-4

The code with problem does the following: It is part of a password reset system that I am creating, but when placing it inside an ECHO I got some problems that I was able to solve but this I see no apparent solution.

echo ''
    .$id = $_GET['id']; // Recebendo o valor vindo do link
    $resultado = $mysqli->query("SELECT * FROM usuarios WHERE ID = '".$id."'"); // Há variável $resultado faz uma consulta na tabela selecionando somente o registro desejado
    while($linha = mysqli_fetch_array($resultado))
    {'
    <html><form id="form_Usuario" method="POST" action="updatecomplete.php" >
        <label> Senha: </label> <br />
            <input type="password" name="pass" style="height:30px; width:250px" value="'.md5($linha['Senha']).'\" />
            <input type="submit" value="Alterar Senha" />
    </form></html>
       '}

When I execute the code I get the following error:

Parse error: syntax error, Unexpected '}' in...

2 answers

2

To display html next to php you must use echo or:

?> seu html aqui <?php

Thus:

while($linha = mysqli_fetch_array($resultado))
{
?>
<html><form id="form_Usuario" method="POST" action="updatecomplete.php" >
    <label> Senha: </label> <br />
        <input type="password" name="pass" style="height:30px; width:250px" value="<?php echo md5($linha['Senha']); ?>" />
        <input type="submit" value="Alterar Senha" />
</form></html>
<?php
}

Probably had problems because here you tried to "escape" the quote (") without need, use so if using echo:

echo ''
    .$id = $_GET['id']; // Recebendo o valor vindo do link
    $resultado = $mysqli->query("SELECT * FROM usuarios WHERE ID = '".$id."'"); // Há variável $resultado faz uma consulta na tabela selecionando somente o registro desejado
    while($linha = mysqli_fetch_array($resultado))
    {

    echo '<html><form id="form_Usuario" method="POST" action="updatecomplete.php" >
        <label> Senha: </label> <br />
            <input type="password" name="pass" style="height:30px; width:250px" value="' . md5($linha['Senha']) . '" />
            <input type="submit" value="Alterar Senha" />
    </form></html>';
    }

I recommend you learn the basics of following the documentation:

0


Missing a period and comma at the end of your string.

</form></html>
   '} <--- aqui

To anticipate and facilitate syntax error indentification, use an IDE or text editor with Highlight.

The code of the question seems to want to make an edit, so only one record is returned, it is not necessary to while, but formatting html and imbutirr php in it is important not to create a messy code.

Do not put direct variables in SQL statement, this makes your code vulnerable to sqlinjections, resolve this with Prepared statements.

<?php
    $id = !empty($_GET['id']) && ctype_digit($_GET['id']) ? $_GET['id'] : 0;
    $stmt = $mysqli->prepare("SELECT * FROM usuarios WHERE ID = ?");
    $stmt->bind_param('i', $id);
    $stmt->execute();
    $result = $stmt->get_result();
    $registro = $result->fetch_assoc();
 ?>   

<html>
    <form id="form_Usuario" method="POST" action="updatecomplete.php" >
        <label> Senha: </label> <br />
        <input type="password" name="pass" style="height:30px; width:250px" value="<?php echo md5($registro['Senha']); ?>" />
        <input type="submit" value="Alterar Senha" />
    </form>
</html>
  • @Guilhermenascimento of good, the answer has some problem? I can improve

  • Sorry for the serious mistakes I made but I am at the beginning of my course in php and sql, this form was very simple even to learn how to deal better with php and I didn’t even use css. About echo at the beginning I used because I had made an if that checked if a generated hash and an email was correct and if it would not show an error and if it existed it would execute this code then what I had read implied that the use of echo was mandatory. Thank you for clarifying my doubt I made some changes in the code and in the end everything went well.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.