phpmysqlpdocontrol-structure

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


I made a simple, posting system. But on the if isset i made it, keeps saying Blank line found at end of control structure But i really cant find the solution and already tried many options, like moving the closing brace etc. The error is on line 26 in this part of the code:


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

If Someone knows the fix for this i would highly apreciate it!

    <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>

Solution

  • Are you sure it's the line 26 and not 10 ? There is one blank line

    <?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.
            ?>
    

    And your code is subject to sql injections

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

    Here you can find more info to prevent hack https://www.php.net/manual/en/security.database.sql-injection.php