问题描述
我收到这些变量的未定义索引错误:id,subj,mid,fin.我已经正确定义了它们,但是我不知道我的代码有什么问题.我认为问题出在我定义这4个变量的代码位置.请帮忙?谢谢.
I'm getting an undefined index error with these variables: id, subj, mid, fin. I have defined them properly and I don't know what's wrong with my code. I think the problem is with the placement of the code where I defined those 4 variables. Please help? Thanks.
echo "
<form action=editgrades.php method=post>
ID: <input type='text' name='id' maxlength='5' size='3'>
Subject: <input type='text' name='subj' maxlength='3' size='3'>
Midterm: <input type='text' name='mid' maxlength='3' size='3'>
Finals: <input type='text' name='fin' maxlength='3' size='3'>
<input type='submit' name='submit' value='Update'>
</form>
";
$mysqli = mysqli_connect("localhost", "root", "", "school");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
} else {
$sql = "select * from studentgrades";
}
$id = $_POST['id'];
$subject = $_POST['subj'];
$midterm = $_POST['mid'];
$finals = $_POST['fin'];
$average = ($midterm + $finals) / 2;
if ($average >= 70) {
$remarks = 'Passed';
} else {
$remarks = 'Failed';
}
$res = mysqli_query($mysqli, $sql);
if ($res) {
$sql1 = "
update studentgrades set Subject = " . $subject . ",
Midterm = " . $midterm . ",
Finals = " . $finals . ",
Average = " . $average . ",
Remarks = " . $remarks . "
where ID = " . $id . "
";
$res1 = mysqli_query($mysqli, $sql1);
if ($res1) {
echo "
Grades updated successfully.
<br><br>
";
}
}
推荐答案
这是您的问题-您无需检查是否设置了帖子.因此,如果您在提交表单之前调用此页面,则您尝试调用的任何$ _POST变量都将具有未定义的索引.
Here's your problem - you don't check if post is set. So if you call this page before submitting a form you will have undefined indices for any $_POST var you try to call.
做这样的事情:
if(isset($_POST['id']))
{
$mysqli = mysqli_connect("localhost", "root", "", "school");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
else{
$sql = "select * from studentgrades";
}
$id = $_POST['id'];
$subject = $_POST['subj'];
$midterm = $_POST['mid'];
$finals = $_POST['fin'];
$average = ($midterm+$finals)/2;
if($average >= 70){
$remarks = 'Passed';
}
else {
$remarks = 'Failed';
}
$res = mysqli_query($mysqli, $sql);
if ($res) {
$sql1 = "
update studentgrades set Subject = ".$subject.",
Midterm = ".$midterm.",
Finals = ".$finals.",
Average = ".$average.",
Remarks = ".$remarks."
where ID = ".$id."
";
$res1 = mysqli_query($mysqli, $sql1);
if ($res1){
echo "
Grades updated successfully.
<br><br>
";
}
}
尽管isset($ _ POST ['id']))可以用多种方式代替,以验证表单是否已提交.
Although isset($_POST['id'])) can be substituted with any number of ways to verify the form was submitted.
一个旁注-您可能应该在输出任何html之前运行所有后期处理,以防您需要根据发布结果修改标头.如果此代码需要执行以下操作:
A side note - you should probably run all of your post processing before any html is output in case you need to modify headers based on the post results. if this code needed to do something like:
if($id == '')
{
header('location: /');
exit;
}
它会告诉您标题已经发送,并给您另一个错误.
it would tell you headers have already been sent and give you another error.
这篇关于PHP未定义的索引/变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!