警告:无法修改标头信息:已发送的标头

时间:2015-11-10 19:42:48

标签: php

我正在尝试使用数据库交互创建几个页面。

这是我的代码,CSS:

<HTML>
<HEAD>
    <TITLE>Modify Employee's Information</TITLE>
    <style type="text/css">
        .form_style {
            margin:10px auto;
            max-width: 400px;
            padding: 20px 12px 10px 20px;
            font: 13px "Lucida Sans Unicode", "Lucida Grande", sans-serif;
        }
        .form_style li {
            padding: 0;
            display: block;
            list-style: none;
            margin: 10px 0 0 0;
        }
        .form_style h1{
            text-align: center;
        }
        .form_style label{
            margin:0 0 3px 0;
            padding:0px;
            display:block;
            font-weight: bold;
        }
        .form_style input[type=text], 
        .form_style input[type=date],
        .form_style input[type=datetime],
        .form_style input[type=number],
        .form_style input[type=search],
        .form_style input[type=time],
        .form_style input[type=url],
        .form_style input[type=email],
        textarea, 
        select{
            box-sizing: border-box;
            -webkit-box-sizing: border-box;
            -moz-box-sizing: border-box;
            border:1px solid #BEBEBE;
            padding: 7px;
            margin:0px;
            -webkit-transition: all 0.30s ease-in-out;
            -moz-transition: all 0.30s ease-in-out;
            -ms-transition: all 0.30s ease-in-out;
            -o-transition: all 0.30s ease-in-out;
            outline: none;  
        }
        .form_style input[type=text]:focus, 
        .form_style input[type=date]:focus,
        .form_style input[type=datetime]:focus,
        .form_style input[type=number]:focus,
        .form_style input[type=search]:focus,
        .form_style input[type=time]:focus,
        .form_style input[type=url]:focus,
        .form_style input[type=email]:focus,
        .form_style textarea:focus, 
        .form_style select:focus{
            -moz-box-shadow: 0 0 8px #88D5E9;
            -webkit-box-shadow: 0 0 8px #88D5E9;
            box-shadow: 0 0 8px #88D5E9;
            border: 1px solid #88D5E9;
        }
        .form_style .field-divided{
            width: 49%;
        }

        .form_style .field-short{
            width: 18.8%;
            text-align: center;
        }

        .form_style .field-salary{
            width:39.4%;
            text-align: center;
        }

        .form_style .field-id{
            width: 10%;
            text-align: center;
        }

        .form_style .field-long{
            width: 90%;
        }
        .form_style .field-select{
            width: 100%;
        }
        .form_style .field-textarea{
            height: 100px;
        }
        .form_style input[type=submit], .form_style input[type=button]{
            background: #4B99AD;
            padding: 8px 15px 8px 15px;
            border: none;
            color: #fff;
            margin: 0 auto;

        }
        .form_style input[type=submit]:hover, .form_style input[type=button]:hover{
            background: #4691A4;
            box-shadow:none;
            -moz-box-shadow:none;
            -webkit-box-shadow:none;
        }
        .form_style .required{
            color:red;
        }
    </style>
</HEAD>
<BODY>

PHP:

    <?php
    include 'connection.php';

    if (!isset($_POST['edit'])) {
        $query = "SELECT * FROM employee_data WHERE emp_id = $_GET[id]";
        $result = mysql_query($query);
        $emp = mysql_fetch_array($result);
    }
    ?>

    <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
        <ul class="form_style">
            <li>
                <h1>Modify Employee's Information</h1>
            </li>
            <li>
                <b>Employee ID:</b>&nbsp;<input type="text" name="id" class="field-id" value="<?php echo $emp['emp_id']?>" readonly/>
            </li>
            <li>
                <label>Employee Name</label>
                <input type="text" name="inputFname" class="field-divided" placeholder="First" value="<?php echo $emp['f_name'] ?>"/>&nbsp;<input type="text" name="inputLname" class="field-divided" placeholder="Last" value="<?php echo $emp['l_name'] ?>"/>
            </li>
            <li>
                Title&nbsp;<input type="text" name="inputTitle" class="field-short" value="<?php echo $emp['title'] ?>"/>&nbsp;Age&nbsp;<input type="text" name="inputAge" class="field-short" value="<?php echo $emp['age'] ?>"/>&nbsp;Year of Services&nbsp;<input type="text" name="inputYos" class="field-short" value="<?php echo $emp['yos'] ?>"/>
            </li>
            <li>
                Salary&nbsp;<input type="text" name="inputSalary" class="field-salary" value="<?php echo $emp['salary'] ?>"/>&nbsp;Perks&nbsp;<input type="text" name="inputPerks" class="field-salary" value="<?php echo $emp['perks'] ?>"/>
            </li>
            <li>
                Email&nbsp;<input type="email" name="inputEmail" class="field-long" value="<?php echo $emp['email'] ?>"/>
            </li>

            <li>
                <input type="submit" name="edit" value="Modify" /> 
                <p>Change any information as you want, or click Modify directly to save without any change.  </p>
                <p>p.s. Employee ID is not changeable.</P>
            </li>
        </ul>
    </form>

    <?php
    if (isset($_POST['edit'])) {
        $edit = "UPDATE employee_data SET f_name ='$_POST[inputFname]',"
                . "l_name ='$_POST[inputLname]',"
                . "title ='$_POST[inputTitle]',"
                . "age ='$_POST[inputAge]',"
                . "yos ='$_POST[inputYos]',"
                . "salary ='$_POST[inputSalary]',"
                . "perks ='$_POST[inputPerks]',"
                . "email ='$_POST[inputEmail]'"
                . "WHERE emp_id = '$_POST[id]'";
        mysql_query($edit) or die(mysql_error());
        header('Location: index.php');
    }
    ?>
</BODY>

我觉得它有点乱,我修复所有功能错误后会更清洁。

所以我不明白为什么它告诉我:

  

警告:无法修改标头信息 - 已在/Applications/XAMPP/xamppfiles/htdocs/Employee/modify.php中发送的标头(在/Applications/XAMPP/xamppfiles/htdocs/Employee/modify.php:128处开始输出)在第166行

第128行只是处理用户ID以将其传递给SQL操作。为什么要发送标题?一切正常,除了页面给我这个错误,并拒绝重定向到主页。

在我向表单添加一些CSS和样式之前,此代码一直在运行。谁能让我知道他们之间的主要区别是什么?

<?php
include 'connection.php';

if (!isset($_POST['edit'])) {
$query = "SELECT * FROM employee_data WHERE emp_id = $_GET[id]";
$result = mysql_query($query);
$emp = mysql_fetch_array($result);
}
?>

<form action = "<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Employee ID<input type="text" name ="id" value="<?php echo $emp['emp_id'] ?>" readonly/> <br />

First Name<input type="text" name="inputFname" value="<?php echo $emp['f_name'] ?>" /> <br />
Last Name<input type="text" name="inputLname" value="<?php echo $emp['l_name'] ?>" /> <br />
Title<input type="text" name="inputTitle" value="<?php echo $emp['title'] ?>" /> <br />
Age<input type="text" name="inputAge" value="<?php echo $emp['age'] ?>" /> <br />
Year of Service<input type="text" name="inputYos" value="<?php echo $emp['yos'] ?>" /> <br />
Salary<input type="text" name="inputSalary" value="<?php echo $emp['salary'] ?>" /> <br />
Perks<input type="text" name="inputPerks" value="<?php echo $emp['perks'] ?>" /> <br />
Email<input type="text" name="inputEmail" value="<?php echo $emp['email'] ?>" /> <br />

<br />
<input type="submit" name="edit" value="Modify The Employee"/> <br />
</form>

<?php
if (isset($_POST['edit'])) {
$edit = "UPDATE employee_data SET f_name ='$_POST[inputFname]',"
. "l_name ='$_POST[inputLname]',"
. "title ='$_POST[inputTitle]',"
. "age ='$_POST[inputAge]',"
. "yos ='$_POST[inputYos]',"
. "salary ='$_POST[inputSalary]',"
. "perks ='$_POST[inputPerks]',"
. "email ='$_POST[inputEmail]'"
. "WHERE emp_id = '$_POST[id]'";
mysql_query($edit) or die(mysql_error());
header("Location: index.php");
}
?>

1 个答案:

答案 0 :(得分:2)

以下错误:

  

警告:无法修改标头信息 - 已发送的标头

如果在发送标头之前已经输出到页面,则会导致

。按照惯例,在显示任何输出之前,必须发送所有header() s(包括cookie)。

要解决此问题,您只需将整个PHP脚本(<?php ... header('Location: index.php'); ?>)移至页面顶部即可。