我正在制作College management system
。它工作正常,一切都显示出来,我可以轻松地将数据插入到我的表单中。但是我无法更新表单,它给了我这个错误:
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
我知道在使用任何header
或位置之前打印任何邮件时,通常会发生此错误。但是如果我在打印任何消息之前使用header
。我的问题将在数据插入下进行,所有内容都将更改为我的表单,即使我无法插入数据。
请帮助解决这里的错误?
这是我的代码:
<?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"> Male</span>
<input type="radio" name="genderrdo" value="Female"<?php if($rs_upd['gender']=="Female") echo "checked";?> /> <span class="p_font"> Female</span>
</div><br>
<div class="teacher_bday_box">
<span class="p_font">Birthday: </span>
<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"> Yes</span>
<input type="radio" name="marriedrdo" value="No"<?php if($rs_upd['married']=="No") echo "checked";?> /> <span class="p_font"> 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" />
<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"> Male</span>
<input type="radio" name="genderrdo" value="Female" /> <span class="p_font"> Female</span>
</div><br>
<div class="teacher_bday_box">
<span class="p_font">Birthday: </span>
<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"> Yes</span>
<input type="radio" name="marriedrdo" value="No"/> <span class="p_font"> 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" />
<input type="reset" href="#" class="btn btn-primary btn-large" value="Cancel" />
</div>
</form>
</div>
</div>
</div>
<?php
}
?>
</body>
</html>
帮助会很感激!!
先谢谢了! :)
答案 0 :(得分:0)
第83行是else $msg="Update Fail!...";
你错过了一个大括号。
else{ $msg="Update Fail!...";
这就是为什么你会收到错误并将错误计为输出(在标题之前)的原因。
else { // <- right there
$msg="Update Fail!...";
}
你也有一个失踪
else
$msg="Insert Error:".mysql_error();
}
所以
else {
$msg="Insert Error:".mysql_error();
}
然后是另一个
if(isset($_POST['btn_upd'])){
应该关闭适当的结束语句,可以是
WHERE teacher_id=$id
");
重写WHERE条件
//------------------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
请自己编写代码编辑器并进行大括号计算。
做像
这样的事情if($sql_update==true)
header("location:?tag=view_teachers");
else {
$msg="Update Fail!...";
}
不是很好的编码习惯;在整个代码中使用适当的支撑技术;它更容易跟踪,并确保支撑对正确匹配。
if($sql_update==true){
header("location:?tag=view_teachers");
}
else {
$msg="Update Fail!...";
}
正如评论中已经提到的,您目前的代码向SQL injection开放。使用prepared statements或PDO with prepared statements。
答案 1 :(得分:0)
回到课堂时,我们总是被告知在php闭括号和doctype之间没有空格,因为已经发送了标题。
要解决此问题,请使用与此类似的代码。
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
并将其更改为
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
始终解决学生在课堂上遇到的问题。