-2

Updating mysql db. This page get an id via page post. The Textfield "Links" could be changed and submitted. After the submit, there's a mysql update on the ID and after the update the page should redirect to dbedit.php. but i get a:

Cannot modify header information - headers already sent on line 13.

I have no idea why? and can't find any output before the "header()" to the user. Where's my mistake?

<html>
<body>
<?php
include('config.php');  
if(isset($_GET['id']))
{
    $id=$_GET['id'];
    if(isset($_POST['submit']))
    {
        $Links=$_POST['Links'];
        $query3=mysqli_query($link, "update posts set Links='$Links' where id='$id'");
        if($query3){
            header('location:dbedit.php');
        }   
    }
    $query1=mysqli_query($link, "select * from posts where ID='$id'");  
    $query2=mysqli_fetch_array($query1);    
?>
<form method="post" action="">
Name:<input type="text" name="Links" value="<?php echo $query2['Links'];?>"/>
<input type="submit" name="submit" value="update" />
</form>
<?php
mysqli_close($link);
};
?>
</body>
</html>
swapfile
  • 415
  • 2
  • 19
  • Am I to assume dbedit.php is a page where I get referred to if the query fails? Seems like a potential security risk, especially not escaped $_GET variables. – Xorifelse Nov 12 '16 at 22:12
  • dbedit.php is not the query fails page. it's the "update is done page". Security risk should be no prob, its just a local host running tool. – swapfile Nov 12 '16 at 22:15

2 Answers2

1

There is output before the header() redirect, its the html and body tags, along with whitespace. The header() needs to come before everything, including the html tag

Bradley4
  • 510
  • 1
  • 6
  • 13
1

If you are using header function there shouldn't be any output before header function. In your case, you used before the header function thats why it is showing you error.

Check out the url to know more why this problem is happening. How to fix "Headers already sent" error in PHP

Change the code in following way:

<?php
        include('config.php');  
        if(isset($_GET['id']))
        {
            $id=$_GET['id'];
            if(isset($_POST['submit']))
            {
                $Links=$_POST['Links'];
                $query3=mysqli_query($link, "update posts set Links='$Links' where id='$id'");
                if($query3){
                    header('location:dbedit.php');
                }   
            }
            $query1=mysqli_query($link, "select * from posts where ID='$id'");  
            $query2=mysqli_fetch_array($query1);    
        ?>    
    <html>
        <body>

        <form method="post" action="">
        Name:<input type="text" name="Links" value="<?php echo $query2['Links'];?>"/>
        <input type="submit" name="submit" value="update" />
        </form>
        <?php
        mysqli_close($link);
        };
        ?>
        </body>
        </html>
Community
  • 1
  • 1
jewelhuq
  • 1,210
  • 15
  • 19