错误 PHP "Blank line found at end of control structure"

ERROR PHP "Blank line found at end of control structure"

我做了一个简单的发帖系统。但是在 if isset 我做到了,一直说 Blank line found at end of control structure 但是我真的找不到解决方案并且已经尝试了很多选项,比如移动右括号等。 错误在第 26 行在这部分代码中:


        if (isset($_POST['like'])) {
            $id = $_POST['like'];
            $query2 = "UPDATE `posts` SET `likes` = likes + 1 WHERE id = '$id'";
            $run = $conn->query($query2);
        }

如果有人知道这个问题的解决方法,我将不胜感激!

    <head>
        <link rel="stylesheet" type="text/css" href="style.css">
    </head>

    <body>
    <?php

    try {
        include 'connection.php';
        $query = $conn->query('SELECT * FROM auteurs INNER JOIN posts ON auteurs.id = posts.auteur_id ORDER BY posts.likes DESC');    
            
        # Haal hier alle posts uit de data base op.
        ?>

    <div class="container">
    <div id='header'>
            <h1>Foodblog</h1>
            <a href='new_post.php'><button>Nieuwe post</button></a>
</span>

    </div>
        <?php
        if (isset($_POST['like'])) {
            $id = $_POST['like'];
            $query2 = "UPDATE `posts` SET `likes` = likes + 1 WHERE id = '$id'";
            $run = $conn->query($query2);
        }
        
        foreach ($query as $info) {
            ?> 
            <div class='post'>
                <div class='header'>
                <h2><?php echo  $info['titel']; ?></h2>
                <img src="<?php echo $info['img_url']; ?>" />
            </div>
                <span class=right>
                <form action='index.php' method='post'>
                <button type='submit'value=<?php echo $info['id']; ?> name='like'>
                <?php echo $info['likes']; ?> likes
                </button>
            </form>
            </span>
                <span class='details'>Geschreven op: <?php echo $info['datum']; ?>  door: <?php echo $info['auteur']; ?>
                </span>
                <p><?php echo $info['inhoud']; ?> </p>
            </div>
            <?php
        }
    } catch (Exception $query) {
        echo $query->getMessage();
    }
    ?>
    </div>
    </body>
</html>

你确定是第 26 行而不是第 10 行吗? 有1个空行

<?php

    try {
        include 'connection.php';
        $query = $conn->query('SELECT * FROM auteurs INNER JOIN posts ON auteurs.id = posts.auteur_id ORDER BY posts.likes DESC');    
            
        # Haal hier alle posts uit de data base op.
        ?>

并且您的代码受到 sql 注入

$query2 = "UPDATE `posts` SET `likes` = likes + 1 WHERE id = '$id'";
            $run = $conn->query($query2);

在这里您可以找到更多信息来防止黑客入侵 https://www.php.net/manual/en/security.database.sql-injection.php