-1

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.

<?php

$servername="localhost";
$username="root";
$password="";
$dbname="online_voting_web";
//Create connection
$con=mysqli_connect($servername,$username,$password,$dbname);

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


if(!$name || !$name2 ||$name3 || $id || !$year || !$pass|| !$username){
$error="Please fill empty fields";
include"student_reg.php";
exit();
}
$sql = "SELECT stud_id FROM stud_name WHERE stud_id='$id'";
$query = mysqli_query($con,$sql)or die(mysqli_error($con));
$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 stud_username FROM stud_user WHERE     username='".$_POST['username']."'";
$query = mysqli_query($con,$sql)or die(mysqli_error($con));
$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 stud_name(stud_id,studFname,stud_Lname,stud_Mname)
     VALUES('$id','$name','$name2','$name3')";
$sql2 = "INSERT INTO stud_course(stud_username,stud_year)
     VALUES('$username','$year')";
$sql3 = "INSERT INTO stud_user(stud_id,stud_username)
     VALUES('$id','$username')"; 
$sql4 = "INSERT INTO login_(username,password)
     VALUES('$username','$pass')";           

  $result = mysqli_query($con,$sql);
   $result2 = mysqli_query($con,$sql2);
 $result3 = mysqli_query($con,$sql3);
 $result4 = mysqli_query($con,$sql4);

if(!$result && !$result2 && !$result3 &&!$result4){
die("Error on mysql query".mysqli_error());
}
else{
header("location: index.php");
}

?>

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

   <!--Style link-->
<link href="layout/styles/layout.css" rel="stylesheet" type="text/css">
<!--Navigation-->
 </div>
</div>
<body id="top">
<div class="wrapper row3">
<div class="rounded">
<main class="container clear"> 
  <div class="group btmspace-30"> 

    <div id="register">
<!--End of Navigation-->
    <fieldset>
        <legend><h3>Student Registration Form</h3></legend>
  <h4 style="color:#e60808;">
 <?php global $nam; echo $nam;?> </h4> 
 <?php global $error; echo $error;?>                  
        <!-- the login form-->
<form enctype="application/x-www-form-urlencoded" action="" method="post" id="myform">
            <div class="register">
            <table width="400px">
 <tr><td><label>Firstname</label></td>
<td><input type="text" name="firstname" value="" style="width:250px;    height:34px; border:1px solid #336666;"/></td>
 </tr>
 <tr><td><label>Middlename</label></td>
 <td><input type="text" name="middlename" value="" style="width:250px; height:34px; border:1px solid #336666;"/></td>
<tr><td><label>Lastname</label></td>
<td><input type="text" name="lastname" value="" style="width:250px; height:34px; border:1px solid #336666;"/></td>
 </tr>
 <tr><td><label>Username</label></td>
  <td><input type="text" name="username" value="" style="width:250px; height:34px; border:1px solid #336666;"/></td>
 </tr>
 <tr><td><label>Password</label></td>
  <td><input type="password" name="password" value="" style="width:250px; height:34px; border:1px solid #336666;"/></td>
</tr>
 <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>
<tr><td><label>ID Number</label></td>
<td><input type="text" name="stud_id" value="" style="width:250px; height:34px; border:1px solid #336666;"/></td>
 </tr>
 <tr><td colspan="2" align="center"><input type="submit" name="submit" value="Submit" style="width:100px; height:34px; border:1px solid #336666; border-radius:4px;"></td></tr>
             </table>
            </div>
 </form>
 <script type="text/javascript">
var frmvalidator = new Validator("myform"); 
frmvalidator.addValidation("firstname","req","Please enter student     firstname"); 
 frmvalidator.addValidation("firstname","maxlen=50");
    frmvalidator.addValidation("middlename","req","Please enter student   middlename"); 
  frmvalidator.addValidation("middlename","maxlen=50");
    frmvalidator.addValidation("lastname","req","Please enter student middlename"); 
   frmvalidator.addValidation("lastname","maxlen=50");
    frmvalidator.addValidation("username","req","Please enter student username"); 
   frmvalidator.addValidation("username","maxlen=50");
   frmvalidator.addValidation("password","req","Please enter student password"); 
   frmvalidator.addValidation("password","minlen=6","Password must not be less than 6 characters.");
    frmvalidator.addValidation("year","req","Please enter student year"); 
   frmvalidator.addValidation("year","dontselect=000","You don't select year");
   frmvalidator.addValidation("stud_id","req","Please enter your ID number"); 
    frmvalidator.addValidation("stud_id","maxlen=50");

    </fieldset>
    </div>

Karolis Koncevičius
  • 9,417
  • 9
  • 56
  • 89
Mals
  • 13
  • 6
  • center,font? the 1990's whats its HTML back –  Sep 05 '16 at 21:44
  • Some sensible code indentation would be a good idea. It help us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](http://www.php-fig.org/psr/psr-2/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly Sep 05 '16 at 21:48
  • Your script is at risk of [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Have a look at what happened to [Little Bobby Tables](http://bobby-tables.com/) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly Sep 05 '16 at 21:49
  • Do you have a `` tag in your HTML? – RiggsFolly Sep 05 '16 at 21:53
  • Yes. My problem was solved. :D and I got another problem haysss. – Mals Sep 05 '16 at 21:58

1 Answers1

0

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

MoeinPorkamel
  • 701
  • 5
  • 8