2015-11-10 103 views
-1

我想創建幾個與數據庫交互的頁面。警告:無法修改標題信息:標題已發送

這裏是我的代碼,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> 

我想這是一個有點亂,我會得到它乾淨多了後,我解決所有功能錯誤。

所以我不明白的是爲什麼它告訴我:

Warning: Cannot modify header information - headers already sent by (output started at /Applications/XAMPP/xamppfiles/htdocs/Employee/modify.php:128) in /Applications/XAMPP/xamppfiles/htdocs/Employee/modify.php on line 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"); 
} 
?> 
+0

你的代碼是完全受到SQL注入攻擊。 – Boann

回答

2

以下錯誤:

Warning: Cannot modify header information - headers already sent

是當發送頭之前已輸出的頁面所導致的。按照慣例,所有header()(包括cookie)必須在任何輸出顯示之前發出。

爲了解決這個問題,你可以簡單地後移至整個PHP腳本(<?php ... header('Location: index.php'); ?>到頁面的頂部。

+1

,您可以停止使用mysql_ * –

+0

謝謝但是問題是:在我添加CSS樣式之前它已經工作了,儘管我做了一些改變,但我不知道它在哪裏影響了這裏的主要問題。 – Tian

相關問題