-3

I'm making a College management system. It's working fine, everything shows perface, I can easily insert the data into my form. But I'm unable to update the form, it gives me this error:

Warning: Cannot modify header information - headers already sent by (output started at 
C:\xampp\htdocs\php_CMS_suman_sudha\Assignment\drop_down_menu.php:83) in C:\xampp\htdocs
\php_CMS_suman_sudha\Assignment\Teachers_Entry.php on line 82

I know this error usually occur when we print any message before using any header or location. But If i use header before printing any message. My problem will go under the data inserting for and everything will be changed into my form, even i can't insert the data.

Please help what's wrong here?

here is my code:

<?php

    $msg="";
    $opr="";
    $id="";
    if(isset($_GET['opr']))
    $opr=$_GET['opr'];

if(isset($_GET['rs_id']))
    $id=$_GET['rs_id'];

//--------------add data-----------------
if(isset($_POST['btn_sub'])){
    $f_name=$_POST['fnametxt'];
    $l_name=$_POST['lnametxt'];
    $gender=$_POST['genderrdo'];
    $dob=$_POST['yy']."/".$_POST['mm']."/".$_POST['dd'];
    $pob=$_POST['pobtxt'];
    $addr=$_POST['addrtxt'];
    $degree=$_POST['degree'];
    $salary=$_POST['slarytxt'];
    $married=$_POST['marriedrdo'];
    $phone=$_POST['phonetxt'];
    $mail=$_POST['emailtxt'];
    $note=$_POST['notetxt'];    

$sql_ins=mysql_query("INSERT INTO teacher_tbl 
                        VALUES(
                            NULL,
                            '$f_name',
                            '$l_name' ,
                            '$gender',
                            '$dob',
                            '$pob',
                            '$addr',
                            '$degree',
                            '$salary' ,
                            '$married',
                            '$phone',
                            '$mail',
                            '$note'
                            )
                    ");
if($sql_ins==true)
    $msg="1 Row Inserted";
else
    $msg="Insert Error:".mysql_error();

}
//------------------uodate data----------
if(isset($_POST['btn_upd'])){
    $f_name=$_POST['fnametxt'];
    $l_name=$_POST['lnametxt'];
    $gender=$_POST['genderrdo'];
    $dob=$_POST['yy']."/".$_POST['mm']."/".$_POST['dd'];
    $pob=$_POST['pobtxt'];
    $addr=$_POST['addrtxt'];
    $degree=$_POST['degree'];
    $salary=$_POST['slarytxt'];
    $married=$_POST['marriedrdo'];
    $phone=$_POST['phonetxt'];
    $mail=$_POST['emailtxt'];
    $note=$_POST['notetxt'];

    $sql_update=mysql_query("UPDATE teacher_tbl SET
                            f_name='$f_name' ,
                            l_name='$l_name' ,
                            gender='$gender' ,
                            dob='$dob' ,
                            pob='$pob' ,
                            address='$addr' ,
                            degree='$degree' ,
                            salary='$salary' ,
                            married='$married' ,
                            phone='$phone' ,
                            email='$mail' ,
                            note='$note'
                        WHERE teacher_id=$id

    ");
if($sql_update==true)
    header("location:?tag=view_teachers");
else
    $msg="Update Fail!...";

}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" href="css/style_entry.css" />
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Welcome to College Management system</title>
<link rel="stylesheet" type="text/css" href="css/style_entry.css" />
</head>

<body>

<?php
if($opr=="upd")
{
    $sql_upd=mysql_query("SELECT * FROM teacher_tbl WHERE teacher_id=$id");
    $rs_upd=mysql_fetch_array($sql_upd);
    list($y,$m,$d)=explode('-',$rs_upd['dob']);
?>

<div class="panel panel-default">
        <div class="panel-heading"><h1><span class="glyphicon glyphicon-user"></span> Teachers Update Form</h1></div>
            <div class="panel-body">
            <div class="container">
                <p style="text-align:center;">Here, you'll update your teachers records into database.</p>
            </div>


<div class="container_form">
    <form method="post">
                <div class="teacher_name_pos">
                    <input type="text" name="fnametxt" class="form-control" value="<?php echo $rs_upd['f_name'];?>" />
                    <input type="text" name="lnametxt" class="form-control" value="<?php echo $rs_upd['f_name'];?>" />
                </div><br>

                <div class="teacher_radio_pos">
                    <input type="radio" name="genderrdo" value="Male"<?php if($rs_upd['gender']=="Male") echo "checked";?> /> <span class="p_font">&nbsp;Male</span>
                    <input type="radio" name="genderrdo" value="Female"<?php if($rs_upd['gender']=="Female") echo "checked";?> /> <span class="p_font">&nbsp;Female</span>
                </div><br>

                <div class="teacher_bday_box">
                    <span class="p_font">Birthday: </span>&nbsp;&nbsp;&nbsp;
                    <div class="select_style">
                        <select name="yy">
                            <option>Year</option>
                            <?php
                            $sel="";
                            for($i=1985;$i<=2015;$i++){ 
                                if($i==$y){
                                    $sel="selected='selected'";}
                                else
                                $sel="";
                            echo"<option value='$i' $sel>$i </option>";
                            }

                        ?>
                        </select>
                    </div>

                    <div class="select_style">
                        <select name="mm">
                            <option>Month</option>
                            <?php
                            $sel="";
                            $mm=array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","NOv","Dec");
                            $i=0;
                            foreach($mm as $mon){
                                $i++;
                                    if($i==$m){
                                        $sel=$sel="selected='selected'";}
                                    else
                                        $sel="";
                                echo"<option value='$i' $sel> $mon</option>";       
                            }
                        ?>                        </select>
                    </div>

                    <div class="select_style">
                        <select name="dd">
                            <option>date</option>
                            <?php
                        $sel="";
                        for($i=1;$i<=31;$i++){
                            if($i==$d)
                            $sel="selected='selected'";
                            else
                                $sel="";
                        ?>
                        <option value="<?php echo $i ;?>"<?php echo $sel ;?> >
                        <?php
                        if($i<10)
                            echo"0"."$i" ;
                        else
                            echo"$i";   

                        ?>
                        </option>   
                        <?php 
                        }?>
                        </select>
                    </div>

                </div><br><br>

                <div class="teacher_bdayPlace_pos">
                    <input type="text" name="pobtxt" class="form-control" value=" <?php echo $rs_upd['pob']; ?>" />
                </div><br>

                <div class="teacher_address_pos">
                    <input type="text" name="addrtxt" class="form-control" value=" <?php echo $rs_upd['address'];?>" />
                </div><br>

                <div class="teacher_degree_pos">
                    <span class="p_font" style="float: left; margin-left: 88px;">Teacher's qualification: </span>
                    <div class="select_style" style="border-left-width: 1px; margin-left: 0px; width: 102px; margin-right: 60px; margin-top: 0px; margin-bottom: 0px;">
                        <select name="degree">
                            <option>Degree</option>
                            <?php
                                $mm=array("Bachelor","Master","P.HD");
                                $i=0;
                                foreach($mm as $mon){
                                    $i++;
                                        if($mon==$rs_upd['degree'])
                                            $iselect="selected";
                                        else
                                            $iselect="";

                                        echo"<option value='$mon' $iselect> $mon</option>";     
                                }
                            ?>                                  
                        </select>
                    </div>
                </div><br>

                <div class="teacher_salary_pos">
                    <input type="text" name="slarytxt" class="form-control" value="<?php echo $rs_upd['salary'];?>" />
                </div><br>

                <div class="teacher_married_pos">
                    <span class="p_font">Married</span>
                    <input type="radio" name="marriedrdo" value="Yes"<?php if($rs_upd['married']=="Yes") echo "checked";?> /> <span class="p_font">&nbsp;Yes</span>
                    <input type="radio" name="marriedrdo" value="No"<?php if($rs_upd['married']=="No") echo "checked";?> /> <span class="p_font">&nbsp;No</span>
                </div><br>

                <div class="teacher_mobile_pos">
                    <input type="text" name="phonetxt" class="form-control" value="<?php echo $rs_upd['phone'];?>" />
                </div><br>

                <div class="teacher_mail_pos">
                    <input type="text" name="emailtxt" class="form-control" value="<?php echo $rs_upd['email'];?>" />
                </div><br>

                <div class="teacher_note_pos">
                    <input type="text" name="notetxt" class="form-control" value="<?php echo $rs_upd['note'];?>" />
                </div><br>

                <div class="teacher_btn_pos">
                    <input type="submit" name="btn_upd" href="#" class="btn btn-primary btn-large" value="Update" />&nbsp;&nbsp;&nbsp;
                    <input type="reset"  href="#" class="btn btn-primary btn-large" value="Cancel" />
                </div>
                                    </form>
            </div>
        </div>
    </div>
<?php   
}
else
{
?>
<div class="panel panel-default">
        <div class="panel-heading"><h1><span class="glyphicon glyphicon-user"></span> Teachers Entry Form</h1></div>
            <div class="panel-body">
            <div class="container">
                <p style="text-align:center;">Here, you'll add new teachers detail to record into database.</p>
            </div>

<div class="container_form">
    <form method="post">
                <div class="teacher_name_pos">
                    <input type="text" name="fnametxt" class="form-control" placeholder="First name" />
                    <input type="text" name="lnametxt" class="form-control" placeholder="Last name" />
                </div><br>

                <div class="teacher_radio_pos">
                    <input type="radio" name="genderrdo" value="Male" /> <span class="p_font">&nbsp;Male</span>
                    <input type="radio" name="genderrdo" value="Female" /> <span class="p_font">&nbsp;Female</span>
                </div><br>

                <div class="teacher_bday_box">
                    <span class="p_font">Birthday: </span>&nbsp;&nbsp;&nbsp;
                    <div class="select_style">
                        <select name="yy">
                            <option>Year</option>
                            <?php
                            for($i=1985;$i<=2015;$i++){ 
                            echo"<option value='$i'>$i</option>";
                            }
                        ?>
                        </select>
                    </div>

                    <div class="select_style">
                        <select name="mm">
                            <option>Month</option>
                            <?php
                            $mm=array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","NOv","Dec");
                            $i=0;
                            foreach($mm as $mon){
                                $i++;
                                echo"<option value='$i'> $mon</option>";        
                            }
                        ?>
                        </select>
                    </div>

                    <div class="select_style">
                        <select name="dd">
                            <option>date</option>
                            <?php
                        for($i=1;$i<=31;$i++){
                        ?>
                        <option value="<?php echo $i; ?>">
                        <?php
                        if($i<10)
                            echo"0".$i;
                        else
                            echo"$i";     
                        ?>
                        </option>   
                        <?php 
                        }?>
                        </select>
                    </div>

                </div><br><br>

                <div class="teacher_bdayPlace_pos">
                    <input type="text" name="pobtxt" class="form-control" placeholder="Place of birth" />
                </div><br>

                <div class="teacher_address_pos">
                    <input type="text" name="addrtxt" class="form-control" placeholder="Address" />
                </div><br>

                <div class="teacher_degree_pos">
                    <span class="p_font" style="float: left; margin-left: 88px;">Teacher's qualification: </span>
                    <div class="select_style" style="border-left-width: 1px; margin-left: 0px; width: 102px; margin-right: 60px; margin-top: 0px; margin-bottom: 0px;">
                        <select name="degree">
                            <option>Degree</option>
                            <?php
                                $mm=array("Bachelor","Master","P.HD");
                                $i=0;
                                foreach($mm as $mon){
                                    $i++;
                                        echo"<option value='$mon'> $mon</option>";
                                    //echo"<option value='$i'> $mon</option>";      
                                }
                            ?>                                          
                        </select>
                    </div>
                </div><br>

                <div class="teacher_salary_pos">
                    <input type="text" name="slarytxt" class="form-control" placeholder="Salary" />
                </div><br>

                <div class="teacher_married_pos">
                    <span class="p_font">Married</span>
                    <input type="radio" name="marriedrdo" value="Yes"/> <span class="p_font">&nbsp;Yes</span>
                    <input type="radio" name="marriedrdo" value="No"/> <span class="p_font">&nbsp;No</span>
                </div><br>

                <div class="teacher_mobile_pos">
                    <input type="text" name="phonetxt" class="form-control" placeholder="Mobile no." />
                </div><br>

                <div class="teacher_mail_pos">
                    <input type="text" name="emailtxt" class="form-control" placeholder="Email address" />
                </div><br>

                <div class="teacher_note_pos">
                    <input type="text" name="notetxt" class="form-control" placeholder="Note" />
                </div><br>

                <div class="teacher_btn_pos">
                    <input type="submit" name="btn_sub" href="#" class="btn btn-primary btn-large" value="Register" />&nbsp;&nbsp;&nbsp;
                    <input type="reset"  href="#" class="btn btn-primary btn-large" value="Cancel" />
                </div>
                                    </form>
            </div>
        </div>
    </div>
<?php
}
?>
</body>
</html>

Help would appreciate!!

Thanks in advanced! :)

2 Answers2

0

Line 83 is this else $msg="Update Fail!..."; you're missing a brace.

So do else{ $msg="Update Fail!..."; that's why you're getting an error and showing the error counts as output (before header).

else { // <- right there
    $msg="Update Fail!...";

}

You also have one missing here

else 
    $msg="Insert Error:".mysql_error();

}

so do

else {
    $msg="Insert Error:".mysql_error();

}

then another in

if(isset($_POST['btn_upd'])){ 

which should be closed for the appropiate closing statement, which could be for

WHERE teacher_id=$id

");

rewrite for the WHERE condition

//------------------uodate data----------
if(isset($_POST['btn_upd'])){
$f_name=$_POST['fnametxt'];
$l_name=$_POST['lnametxt'];
$gender=$_POST['genderrdo'];
$dob=$_POST['yy']."/".$_POST['mm']."/".$_POST['dd'];
$pob=$_POST['pobtxt'];
$addr=$_POST['addrtxt'];
$degree=$_POST['degree'];
$salary=$_POST['slarytxt'];
$married=$_POST['marriedrdo'];
$phone=$_POST['phonetxt'];
$mail=$_POST['emailtxt'];
$note=$_POST['notetxt'];

$sql_update=mysql_query("UPDATE teacher_tbl SET
                        f_name='$f_name' ,
                        l_name='$l_name' ,
                        gender='$gender' ,
                        dob='$dob' ,
                        pob='$pob' ,
                        address='$addr' ,
                        degree='$degree' ,
                        salary='$salary' ,
                        married='$married' ,
                        phone='$phone' ,
                        email='$mail' ,
                        note='$note'
                    WHERE teacher_id=$id

");
} // <- another missing one

Do get yourself a code editor and do a brace count.

Doing stuff like

if($sql_update==true)
    header("location:?tag=view_teachers");
    else {
        $msg="Update Fail!...";

    }

is not good coding practice; use proper bracing techniques throughout your code; it's a lot easier to keep track of, and ensures brace pairs are properly matched.

if($sql_update==true){
    header("location:?tag=view_teachers");
}
    else {
        $msg="Update Fail!...";

    }

As already noted in comments, your present code is open to SQL injection. Use prepared statements, or PDO with prepared statements.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Still getting the same error.. :( – mehir mehta Aug 22 '14 at 13:38
  • Here is an error now.. [image](http://postimg.org/image/g59uriqu5/) – mehir mehta Aug 22 '14 at 13:40
  • @mehirmehta I have edited my answer since, so do reload it. You'll have to go through your entire code to ensure that all braces `{}` are properly paired up. – Funk Forty Niner Aug 22 '14 at 13:42
  • @mehirmehta The missing brace for `if(isset($_POST['btn_upd'])){` that's most likely why. Reload answer under that body of code. Again, use proper bracing techniques. I have no way to test your code, you will need to go over my entire answer and read the reasons why. – Funk Forty Niner Aug 22 '14 at 13:46
  • Thanks for trying but now it's giving me this [error](http://postimg.org/image/s6wx3jx8j/). :( – mehir mehta Aug 22 '14 at 13:49
  • @mehirmehta You have `href="#"` in both ` – Funk Forty Niner Aug 22 '14 at 13:51
  • I did ..still getting the same error :( – mehir mehta Aug 22 '14 at 13:55
  • @mehirmehta You will need to debug your code. As far as I'm concerned, what I've done to outline where the "Headers already sent" error was coming from, have answered this question. There's nothing more that I can do at this point, it's up to you to figure out why it's not set. – Funk Forty Niner Aug 22 '14 at 13:58
0

Back in class we were always told to not have spaces between the php closing brace and the doctype because of headers already sent.

To remedy this take your code that looks like this.

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

And change it to

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

Always fixed the problems students had in class.

Dustin Harrell
  • 457
  • 1
  • 5
  • 13
  • I'm getting this [error](http://postimg.org/image/s6wx3jx8j/). – mehir mehta Aug 22 '14 at 13:48
  • I'd try to echo out every variable for $_POST['']; To see if any of your inputs are not there Id go as far to wager you don't have $dob=$_POST['yy']."/".$_POST['mm']."/".$_POST['dd']; Line 17 $phone=$_POST['phonetxt']; Line 23 – Dustin Harrell Aug 22 '14 at 20:46