我有sql包括:idChecklist,descriptiveTitle,courseNo,lec,lab,units,year,semester。现在我的代码运行时没有错误,但我无法编辑我想编辑的单词。它只是在数据库中查看我的数据。当我点击编辑并按保存没有任何反应。 在我的代码中编辑错过了什么?谢谢
editSubject.php
<?php
include 'connection.php';
$id=$_GET['id'];
?>
<html>
<title>Edit Subjects</title>
</head>
<body>
<form method="post">
<table>
<?php $checklist_query=mysql_query("select * from checklist where idChecklist='$id'");
$checklist_rows=mysql_fetch_array($checklist_query);
?>
<tr><td>Course:</td><td><input type="text" name="course" value="<?php echo $checklist_rows['course']; ?>"></td></tr>
<tr><td>Descriptive Title:</td><td><input type="text" name="descriptiveTitle" value="<?php echo $checklist_rows['descriptiveTitle']; ?>"></td></tr>
<tr><td>Course No:</td><td><input type="text" name="courseNo" value="<?php echo $checklist_rows['courseNo']; ?>"></td></tr>
<tr><td>Lec:</td><td><input type="text" name="lec" value="<?php echo $checklist_rows['lec']; ?>" ></td></tr>
<tr><td>Lab:</td><td><input type="text" name="lab" value="<?php echo $checklist_rows['lab']; ?>" ></td></tr>
<tr><td>Units:</td><td><input type="text" name="units" value="<?php echo $checklist_rows['units']; ?>" ></td></tr>
<tr><td>Year:</td><td><input type="text" name="year" value="<?php echo $checklist_rows['year']; ?>" ></td></tr>
<tr><td>Semester:</td><td><input type="text" name="semester" value="<?php echo $checklist_rows['semester']; ?>" ></td></tr>
<tr><td></td><td><input type="submit" name="submit" value="save"></td></tr>
</table>
</form>
</body>
</html>
<?php
if (isset($_POST['submit'])){
$course=$_POST['course'];
$descriptiveTitle=$_POST['descriptiveTitle'];
$courseNo=$_POST['courseNo'];
$lec=$_POST['lec'];
$lab=$_POST['lab'];
$units=$_POST['units'];
$year=$_POST['year'];
$semester=$_POST['semester'];
mysql_query("update subjectschedule set course='$course',descriptiveTitle='$descriptiveTitle',courseNo='$courseNo',lec='$lec' ,lab='$lab' ,units='$units' ,year='$year' ,semester='$semester'where idChecklist='$id'");
header('location:checklist.php');
}
?>
Checklist.php
<!doctype html>
<html>
<head>
<body>
<a href="index.php">Back</a>
<a href="addSubject.php">AddSubject</a>
<p>Sort by:</p>
Course<select id = "course">
<option value="">----</option>
<?php
error_reporting(E_ALL ^ E_DEPRECATED);
require "connection.php";
$sql="Select course from checklist group by course;";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
echo '<option value="'.$row["course"].'">'.$row["course"].'</option>';
}
?>
</select>
Year<select id = "year">
<option value="">----</option>
<?php
error_reporting(E_ALL ^ E_DEPRECATED);
require "connection.php";
$sql="select year from checklist group by year;";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
echo '<option value="'.$row["year"].'">'.$row["year"].'</option>';
}
?>
</select>
Semester<select id = "semester">
<option value="">----</option>
<?php
error_reporting(E_ALL ^ E_DEPRECATED);
require "connection.php";
$sql="Select semester from checklist group by semester;";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
echo '<option value="'.$row["semester"].'">'.$row["semester"].'</option>';
}
?>
</select>
<table border="1">
<?php
$checklist_query=mysql_query("select * from checklist");
while($checklist_rows=mysql_fetch_array($checklist_query)){
?>
<tr>
<td><?php echo $checklist_rows['course'] ; ?></td>
<td><?php echo $checklist_rows['descriptiveTitle'] ; ?></td>
<td><?php echo $checklist_rows['courseNo'] ; ?></td>
<td><?php echo $checklist_rows['lec'] ; ?></td>
<td><?php echo $checklist_rows['lab'] ; ?></td>
<td><?php echo $checklist_rows['units'] ; ?></td>
<td><?php echo $checklist_rows['year'] ; ?></td>
<td><?php echo $checklist_rows['semester'] ; ?></td>
<td><a href="viewSubjectSchedule.php<?php echo '?id=' .$checklist_rows['idChecklist']; ?>">Schedule/s</a></td>
<td><a href="editSubject.php<?php echo '?id='.$checklist_rows['idChecklist']; ?>">Edit</a></td>
<td><a href="deleteSubject.php<?php echo '?id='.$checklist_rows['idChecklist']; ?>">Delete</a></td>
</tr>
<?php }?>
</body>
</html>
答案 0 :(得分:0)
在向浏览器输出一些文本/ HTML后,你正在调用header(),这是禁忌。所以你必须把这个块放在代码的顶部。
if (isset($_POST['submit'])){
...
}
并且'require'连接有两个重复的连接.php“;'在checklist.php中
在更新查询中之前没有空格!你需要用空格分隔单词。 学期='$ semester'where
你应该使用mysql_real_scape_string()来阻止sql注入。例如:
$semester = mysql_real_scape_string($_POST['semester']);
或当你知道它是整数时:
$id = (int) $_GET['id'];
使用mysqli或PDO代替mysql是一个好习惯,因为它已被弃用。