PHP标头不工作
我有一个web页面,它将使用POST方法从另一个页面获取数据,然后将数据存储到数据库中。完成后,我希望它返回到数据来源的页面。我使用了header()。但它不起作用。 这是我的密码:PHP标头不工作,php,Php,我有一个web页面,它将使用POST方法从另一个页面获取数据,然后将数据存储到数据库中。完成后,我希望它返回到数据来源的页面。我使用了header()。但它不起作用。 这是我的密码: <?php $date = $_POST['date']; $spent = $_POST['spent']; $description = $_POST['description']; $dbc = mysqli_connect('oniddb.cws.oregonstate.edu', 'chentao-
<?php
$date = $_POST['date'];
$spent = $_POST['spent'];
$description = $_POST['description'];
$dbc = mysqli_connect('oniddb.cws.oregonstate.edu', 'chentao-db', 'BUU3UuwwGfwt7hxV', 'chentao-db')
or die(mysql_error());
$query = "SELECT Total FROM Expense E ORDER BY E.Index DESC LIMIT 1";
//get the total
$result = mysqli_query($dbc, $query) or die(mysql_error());
$total = mysqli_fetch_array($result, MYSQL_ASSOC);
$total = $total['Total'] + $spent;
$query = "INSERT INTO Expense (Date, Spent, Total, Description) VALUES ('$date', '$spent', '$total', '$description')";
$result = mysqli_query($dbc, $query) or die(mysql_error());
mysql_close($dbc);
header("Location: fetchData.php");
我想它会像这样工作
header("Location: http://".$_SERVER['HTTP_HOST']."/fetchData.php");
设法安置
ob_start();
在您的文件顶部
或者你可以试试
header('Location: '.$_SERVER['HTTP_REFRER']);
die;
不要忘记最后放置骰子。如果您的第一个查询没有返回任何结果,那么执行将在此行失败:
$total = $total['Total'] + $spent;
检查第一个查询是否有如下结果:
if(isset($total['Total'])){
$total = $total['Total'] + $spent;
}
else {
$total = $spent;
}
它是如何失败的?没有错误?没有奇怪的行为?无任何内容?是上一页fetchData.php
?请尝试使用完整路径http://domain.com/fetchData.php
从代码中删除结束语?>
,然后重试。你可能在?>
后面有一行额外的内容。仔细阅读这篇文章,它会帮助你:“Maybe”不是答案。