mardi 30 décembre 2014

Program working but still can't get rid of headers already sent error


Vote count:

0




I am making a simple box that takes input and prints it to the screen.


The program errors out whenever I run it due to the "headers already sent" error but when I go back and refresh the shout box my input is there, so the database is being connected to and doing what it is supposed to do, how do I lose the header error? Its on line 26...



<?php
include 'database.php';
?>
<?php
//check if form is submitted
if (isset($_POST['submit'])) {
$user = mysqli_real_escape_string($conn, $_POST['user']);
$message = mysqli_real_escape_string($conn, $_POST['message']);

//set timezone
date_default_timezone_set('America/Edmonton');
$time = date('h:i:s a', time());

//validate input
if(!isset($user) || $user == '' || !isset($message) || $message == '') {
$error = "Please fill in your name and a message";
header("Location: index.php?error=".urlencode($error));
exit();
} else {
$query = "INSERT INTO shouts(time, user, message)
VALUES ('$time','$user', '$message')";

if(!mysqli_query($conn, $query)) {
die('Error: '.mysqli_error($conn));
} else {
header("Location: index.php");
exit();
}
}
}
?>


since it calls it I will add database.php



<?php

// connect to the server
$conn = mysqli_connect("$hostname", "$username", "$password", "$dbname");

// check connection
if (mysqli_connect_errno()) {
exit('Connect failed: '. mysqli_connect_error());
}

?>


I removed the connection variables on purpose, the connection to the DB is not the problem. I hope I added the code right, this is my first post...



asked 56 secs ago







Program working but still can't get rid of headers already sent error

Aucun commentaire:

Enregistrer un commentaire