标题不会重定向未设置不会取消设置

时间:2018-06-02 12:15:41

标签: php session redirect mysqli

我有这个代码工作正常,我似乎无法使用UNSET命令和header命令。

我试图在我的代码中移动它们但没有运气。语法不能错(我认为)命令的位置是否存在任何错误?

该功能正常,我收到成功消息,但没有重定向。我在教程中看到有人使用包含“../html/Search.php”;但是当我尝试这样做时,搜索页面会出现在页面中间。

这是我的代码:

function UpdateCompanies($connection, $tax, $address, $city, $country, $status, $website, $company_foundation ) {
 $n = mysqli_real_escape_string($connection, $name);
 $t = mysqli_real_escape_string($connection, $tax);
 $a = mysqli_real_escape_string($connection, $address);
 $c = mysqli_real_escape_string($connection, $city);
 $ct= mysqli_real_escape_string($connection, $country);
 $s= mysqli_real_escape_string($connection, $status);
 $w= mysqli_real_escape_string($connection, $website);
 $f= mysqli_real_escape_string($connection, $company_foundation);
 $e= mysqli_real_escape_string($connection, $company_employees);

 $query = "UPDATE `seta`.`companies` SET `tax_number`='$t',`street`='$a', `city`='$c', `country_id`='$ct',`status`='$s',`website`='$w',`foundation_year`='$f', `employees`='$e'  WHERE `company_id`='".$_SESSION["company_mod"]."'  ;";


  if ($connection->query($query) === TRUE) {

    echo "New record Updated successfully.";
    header('Location: Search.php');
    unset($_SESSION['company_mod']);

 } else {
    echo "Error: " . $query . "<br>" . $connection->error;
}
}

请注意,我保证,只要我让这部分代码正常工作,我就会清理更多变量。

2 个答案:

答案 0 :(得分:0)

echo之后你无法发送标题! 也许你可以使用这段代码

header( "Refresh:5; url=http://www.example.com/page2.php", true, 303);
echo "New record Updated successfully.";
unset($_SESSION['company_mod']);

另外,我建议你删除MySQL的错误信息。因为这可能是安全漏洞

答案 1 :(得分:-1)

您可以尝试使用此代码吗?:

function UpdateCompanies($connection, $tax, $address, $city, $country, $status, $website, $company_foundation ) {
 $n = mysqli_real_escape_string($connection, $name);
 $t = mysqli_real_escape_string($connection, $tax);
 $a = mysqli_real_escape_string($connection, $address);
 $c = mysqli_real_escape_string($connection, $city);
 $ct= mysqli_real_escape_string($connection, $country);
 $s= mysqli_real_escape_string($connection, $status);
 $w= mysqli_real_escape_string($connection, $website);
 $f= mysqli_real_escape_string($connection, $company_foundation);
 $e= mysqli_real_escape_string($connection, $company_employees);

 $query = "UPDATE `seta`.`companies` SET `tax_number`='$t',`street`='$a', `city`='$c', `country_id`='$ct',`status`='$s',`website`='$w',`foundation_year`='$f', `employees`='$e'  WHERE `company_id`='".$_SESSION["company_mod"]."'  ;";


  if ($connection->query($query) === TRUE) {

    echo "New record Updated successfully.";
    unset($_SESSION['company_mod']);
    echo "<script type='text/javascript'>window.location.replace='http://example.com/';</script>"; 
    exit;

 } else {
    echo "Error: " . $query . "<br>" . $connection->error;
 }
}