PHP While输入字段->;分贝
我有一个问题,我不知道如何解决它。 我有一段时间为表中的每一行创建一个表单。现在我想编辑一个字段,并将数据库中的数据更改为字段中的数据PHP While输入字段->;分贝,php,Php,我有一个问题,我不知道如何解决它。 我有一段时间为表中的每一行创建一个表单。现在我想编辑一个字段,并将数据库中的数据更改为字段中的数据 function Grades($id,$sem){ echo "<h3>Grades - Sem $sem</h3>"; $sth = $this->dbh->prepare("SELECT id, grade, subject, date, sem FROM grades W
function Grades($id,$sem){
echo "<h3>Grades - Sem $sem</h3>";
$sth = $this->dbh->prepare("SELECT id, grade, subject, date, sem
FROM grades
WHERE id_student = :id AND
sem = :sem");
$sth->bindParam(":id", $id);
$sth->bindParam(":sem", $sem);
$sth->execute();
while ($result = $sth->fetch(PDO::FETCH_ASSOC)) {
echo "<form action='students.php?change-students' method='post'>";
echo "<br>Subject ".$result['subject']." Grade ".$result['grade']." Date ".$result['date']." Sem ".$result['sem']."<br>";
echo "<input type='text' name='grade' value='$result[grade]'>";
echo "<input type='submit' value='Submit'>";
echo "</form>";
}
if (isset($_POST['grade'])) {
echo $_POST['grade'];
$sth = $this->dbh->prepare("UPDATE grades
SET grade = :grade
WHERE id_student = :id AND
sem = :sem");
$sth->bindParam(":grade", $_POST['grade']);
$sth->bindParam(":id", $id);
$sth->bindParam(":sem", $sem);
$sth->execute();
}
}
功能等级($id,$sem){
echo“等级——Sem$Sem”;
$sth=$this->dbh->prepare(“选择id、等级、主题、日期、sem
分级
其中id_student=:id和
sem=:sem”);
$sth->bindParam(“:id”,$id);
$sth->bindParam(“:sem”,$sem);
$sth->execute();
而($result=$sth->fetch(PDO::fetch_ASSOC)){
回声“;
回显“
主题“$result['Subject']”等级“$result['Grade']”日期“$result['Date']”扫描电镜“$result['Sem']”等级“
”;
回声“;
回声“;
回声“;
}
如果(isset($_POST['grade'])){
echo$_POST['grade'];
$sth=$this->dbh->prepare(“更新等级
设置坡度=:坡度
其中id_student=:id和
sem=:sem”);
$sth->bindParam(“:grade”,$_POST['grade']);
$sth->bindParam(“:id”,$id);
$sth->bindParam(“:sem”,$sem);
$sth->execute();
}
}
问题是我不知道如何使name=“grade”唯一并在if-isset中使用它。当然,我可以更改name=“$result[id]”,但我不知道如何在isset内部使用($\u POST[''$result['id'])
谢谢试试这个
echo "<input type='text' name=".( isset($_POST[$result['id'])?$_POST[$result['id']):"grade")."value='$result[grade]'>";
echo”“;
或者更好:
$tGrade = isset($_POST[$result['id'])? $_POST[$result['id']):"grade";
echo "<input type='text' name=".$tGrade."value='$result[grade]'>";
$tGrade=isset($\u POST[$result['id'])?$\u POST[$result['id']):“grade”;
回声“;
执行以下操作可能会有所帮助
只需将成绩id作为隐藏字段传递到表单中
echo "<input type='hidden' name='id' value='$result[id]'>";
小心…使用
htmlspecialchars()
围绕HTML上下文中使用的任何任意数据,以确保生成有效的HTML并防止XSS攻击。谢谢……我将更改我的代码您试图让一个函数处理所有内容。不要这样做。一个函数用于生成表单。一个函数用于处理更新。另一个函数用于将所有内容组合在一起并在黑暗束缚着他们。
if (isset($_POST['grade'])) {
$sth = $this->dbh->prepare("UPDATE grades
SET grade = :grade
WHERE id= :id // this will be your hidden post id(i.e. $_POST['id'])