-1

I am trying to create a few pages with database interaction.

Here is my code, 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>

I guess it is a bit messy and I will get it much cleaner after I fix all the functional bugs.

So what I don't understand is why is it telling me:

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

The line 128 is just handling the user ID for passing by it to the SQL action. Why is it sending the header? Everything works fine except the page gives me this error and refuses to redirect to the main page.

This code had been working before I added some CSS and style to the form. Could anyone give me an idea what was the major differences about them?

<?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");
}
?>
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Tian
  • 171
  • 1
  • 1
  • 4

1 Answers1

2

The following error:

Warning: Cannot modify header information - headers already sent

is caused when there has already been output to the page before a header is sent. By convention, all header()s (including cookies) must be sent out before any output is displayed.

To fix this, you can simply move your entire PHP script (<?php ... header('Location: index.php'); ?> to the top of the page.

Jonathan Lam
  • 16,831
  • 17
  • 68
  • 94
  • 1
    after that, you can stop using mysql_* –  Nov 10 '15 at 19:59
  • Thank you for you help. But the thing is : It has been work before I added the css styles. Although I have made a few changes but I don't know where it affects the major problem here. – Tian Nov 10 '15 at 20:00