swapfile swapfile - 25 days ago 5
PHP Question

PHP MYSQL update / redirect

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>

Answer

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>