0

Possible Duplicate:
Headers already sent by PHP

I have a database and when I sumit info, i get this error:

"Warning: Cannot modify header information - headers already sent by (output started at /Applications/XAMPP/xamppfiles/htdocs/mostra/html/noticias/publicacomentario.php:2) in /Applications/XAMPP/xamppfiles/htdocs/mostra/html/noticias/publicacomentario.php on line 14"

I would like rest in the same page When I insert values to database, this my code:

VER.PHP

<form action="noticias/publicacomentario.php" method="post" id="commentform"  onsubmit="MM_validateForm('usuario','','R','email','','RisEmail','comentario','','R');return document.MM_returnValue">
<fieldset>
    <input type="hidden" name="noticia_id" value="<?php echo $id;  ?>"><br>
    <p><label>NOMBRE *</label>
    <input type="text" name="usuario"></p>
    <p><label for="email">EMAIL (No se publicará) *</label>
    <input type="text" name="email"></p>

    <p><label for="comment">COMENTARIO</label>
    <textarea name="comentario" id="comment" cols="100%" rows="10" tabindex="4"></textarea></p>
    <p><input type="submit" name="submit" id="submit" tabindex="5" value="Enviar " /></p>
</fieldset>
</form>

PUBLICARCOMENTARIO.PHP

<?php

require ('connect.php');
$id=$_POST['noticia_id'];
$nick=$_POST['usuario']; 
$email=$_POST['email']; 
$comentario=$_POST['comentario']; 
$query = "INSERT INTO comentarios (usuario,email,comentario,noticia_id, fecha) VALUES('$nick','$email','$comentario','$id', NOW())";
mysql_query($query) or die(mysql_error());

$query = "UPDATE  noticias SET num_comentarios= num_comentarios+1 where id_noticia='".$id."'";
mysql_query($query) or die(mysql_error());
header("Location: ver.php?id=$id")

?>

Any idea? thx!

Community
  • 1
  • 1
berti
  • 113
  • 3
  • 14
  • perhaps connect.php is sending content already? Don't suppose you could post that file as well... – Kiirani Dec 09 '12 at 22:23
  • @Kiirani It won't be that, otherwise the error would show connect.php as the file where the original output came from. Chances are the code posted is not accurate. – Michael Blake Dec 09 '12 at 22:27
  • 3
    **WARNING!** Your code contains an [SQL injection vulnerability](http://en.wikipedia.org/wiki/SQL_injection). Please [switch to PDO](http://php.net/book.pdo) or [mysqli](http://php.net/book.mysqli) so you can use [prepared statements with parameterized queries](http://en.wikipedia.org/wiki/Prepared_statement). – Charles Dec 09 '12 at 22:30
  • si , is sending, into the database are the new values inserted, but the redirection dosen't work – berti Dec 09 '12 at 22:31
  • @ Michael - Herpderp, right :P – Kiirani Dec 10 '12 at 00:23

4 Answers4

1

It says line 2, but there's nothing actually on line 2 of PUBLICARCOMENTARIO.PHP.

Ensure that you haven't got any spaces or new lines above "PUBLICARCOMENTARIO.PHP" before the PHP opening tag.

If PUBLICARCOMENTARIO.PHP only contains PHP (No HTML), then you shouldn't require the closing ?> either.

Basically the error is saying that the header("xxx") can't be output as you've output something else already. Usually it's some HTML or some space before the opening PHP tag.

Michael Blake
  • 135
  • 2
  • 9
0

you should remove blank lines before "<?php" in PUBLICARCOMENTARIO.PHP

dimaninc
  • 765
  • 5
  • 16
0

Check if PUBLICARCOMENTARIO.PHP is saved as UTF8+BOM and if yes, remove the BOM. You can do this with Notepad++ or any other good editor.

Imanuel
  • 3,596
  • 4
  • 24
  • 46
0

I put the PUBLICARCOMENTARIO.PHP into VER.PHP

                                <form action="" method="post" id="commentform"  onsubmit="MM_validateForm('usuario','','R','email','','RisEmail','comentario','','R');return document.MM_returnValue">
                                        <fieldset>
                                        <input type="hidden" name="noticia_id" value="<?php echo $id;  ?>"><br>
                                            <p><label>NOMBRE *</label>
                                            <input type="text" name="usuario"></p>
                                            <p><label for="email">EMAIL (No se publicará) *</label>
                                            <input type="text" name="email"></p>

                                            <p><label for="comment">COMENTARIO</label>
                                            <textarea name="comentario" id="comment" cols="100%" rows="10" tabindex="4"></textarea></p>
                                            <p><input type="submit" name="submit" id="submit" tabindex="5" value="Enviar " /></p>

                                        </fieldset>
                                    </form>
                                </div>



                                <?php
if ($_POST) {
require ('connect.php');
$id=$_POST['noticia_id'];
$nick=$_POST['usuario']; 
$email=$_POST['email']; 
$comentario=$_POST['comentario']; 
$query = "INSERT INTO comentarios (usuario,email,comentario,noticia_id, fecha) VALUES('$nick','$email','$comentario','$id', NOW())";
mysql_query($query) or die(mysql_error());

$query = "UPDATE  noticias SET num_comentarios= num_comentarios+1 where id_noticia='".$id."'";
mysql_query($query) or die(mysql_error());
}
header("Location: $_SERVER[PHP_SELF]?id=$id");
?>

Inside the HEADER("location: ....) i put the $_SERVER[PHP_SELF].

With this works.. but the values ​​entered are not displayed until you refresh the page.. why??

berti
  • 113
  • 3
  • 14