我正在创建一个简单的框,它接受输入并将其打印到屏幕上。
由于“已发送标头”错误,每当我运行它时程序都会出错,但是当我回去刷新喊话框时我的输入就在那里,所以数据库正在连接并执行它应该做的事情,如何丢失标题错误?它在第26行...我已经看到了其他帖子,但是当运行process.php时,在标题之前没有html或任何echo类型的语句,所以不应该有效吗?
<?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();
}
}
}
?>
因为它调用它我将添加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());
}
?>
我故意删除了连接变量,与DB的连接不是问题。我希望我添加正确的代码,这是我的第一篇文章...