You haven't validated your input. Hence the issue.
Validate and sanitize!
<?php
include 'db.php';
if(isset($_POST['search'])) {
$_POST['stYear'] = array_key_exists('stYear', $_POST) ? mysqli_real_escape_string($con, $_POST['stYear']) : null;
if (empty($_POST['stYear'])) {
exit('Invalid year given');
}
$sql = "SELECT * FROM socio WHERE year ='$input'";
$res = mysqli_query($con, $sql) or die($sql);
while ($row = mysqli_fetch_array($res)) {
echo 'You selected '.$input;
}
mysqli_close($con);
}
Also, if you only intend to use the years you've provided in your form, you can whitelist them by adding in_array($_POST['stYear'], ['2014-15', '2015-16', '2016-17', '2017-18', '2018-19'])
.
Example below:
<?php
include 'db.php';
if(isset($_POST['search'])) {
$_POST['stYear'] = array_key_exists('stYear', $_POST) && in_array($_POST['stYear'], ['2014-15', '2015-16', '2016-17', '2017-18', '2018-19']) ? $_POST['stYear'] : null;
if (empty($_POST['stYear'])) {
exit('Invalid year given');
}
$sql = "SELECT * FROM socio WHERE year ='$input'";
$res = mysqli_query($con, $sql) or die($sql);
while ($row = mysqli_fetch_array($res)) {
echo 'You selected '.$input;
}
mysqli_close($con);
}