您尚未驗證您的輸入。因此,這個問題。 驗證和消毒!
<?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);
}
另外,如果你只打算使用你已經在你的形式提供這些年來,你可以通過添加白名單in_array($_POST['stYear'], ['2014-15', '2015-16', '2016-17', '2017-18', '2018-19'])
他們。 以下示例:
<?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);
}
'$ _GET'或'$ _POST'?你也開放SQL注入。此外,如果您只需要「社會」列,則只應選擇該列。 – chris85
[:「注意:未定義變量」和「通知:未定義的索引」 PHP]的可能的複製(http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – chris85
嗨我不知道在哪裏使用$ _GET或$ _POST變量也socio是表 – Krish