Mals Mals - 2 months ago 31
PHP Question

php dropdownlist undefined index

I'm a newbie to PHP programming and I am in the module of using dropdown list. A PHP Error was encountered Severity: Notice Message: Undefined index: yos ........

here's the code. I hope somebody help me.


include "connection.php";
$con = db();

$name = $_POST['firstname'];
$name2 = $_POST['lastname'];
$name3 = $_POST['username'];
$pass = md5($_POST['password']);
$course = $_POST['course'];
$yos = $_POST['yos'];
$id = $_POST['stud_id'];


if(!$name || !$id || !$yos || !$course || !$name2){
$error="Please fill empty fields";
include"student_reg.php";
exit();
}
$sql = "SELECT stud_id FROM student WHERE stud_id='$id'";
$query = mysqli_query($con,$sql);
$exist = mysqli_num_rows($query);

if($exist==1){
$nam="<center><h4><font color='#FF0000'>The student ID already exist,register another</h4></center></font>";
unset($id);
include('student_reg.php');
exit();
}
$sql = "SELECT username FROM login WHERE username='".$_POST['username']."'";
$query = mysqli_query($con,$sql);
$exist = mysqli_num_rows($query);

if($exist==1){
$nam="<center><h4><font color='#FF0000'>The student ID already exist,register another</h4></center></font>";
unset($id);
include('student_reg.php');
exit();

}


$sql = 'INSERT INTO student(stud_id,firstname,lastname,username,course,yos)
VALUES("'.$_POST['stud_id'].'","'.$_POST['firstname'].'","'.$_POST['lastname'].'","'.$_POST['username'].'","'.$_POST['course'].'","'.$_POST['yos'].'")';

$sql2 = 'INSERT INTO login(username,password)
VALUES("'.$_POST['username'].'","'.md5($_POST['password']).'")';

$result = mysqli_query($con,$sql);
$result2 = mysqli_query($con,$sql2);

if(!$result && !$result2){
die("Error on mysql query".mysql_error());
}else{
header("location: login.php");
}

?>


........

And this is the part of the code from another form where I first use the word yos
........

<tr>
<td><label>Year Level</label></td>
<td><select name="yos" id="yos" style="width:250px; height:34px; border:1px solid #336666;">
<option value="000">Choose Level</option>
<option value="G7">Grade 7</option>
<option value="G8">Grade 8</option>
<option value="G9">Grade 9</option>
<option value="G10">Grade 10</option>
<option value="G11a">Grade 11-GAS</option>
<option value="G11b">Grade 11-Blhablha</option>
<option value="G12a">Grade 12-Blahblah</option>
<option value="G12b">Grade 12-khfakhf</option>
</select></td>
</tr>

Answer

You should make sure key exists in $_POST, then assign it to a variable:

$yos = isset($_POST['yos']) ? $_POST['yos'] : '';

Also its not a error, its just a Notice Message which you can skip it

Comments