在浏览器源代码中手动更改值后,数据库表中的未显示值

时间:2017-09-25 12:49:42

标签: php database security mysqli insert-into

我有一个包含2个选择元素的表单 - 从数据库表中选择1个包含5个国家/地区' landen',
- 从DB table' gerechten'中选择2道15道菜 选择2取决于选择1.(我为此使用AJAX)
提交后,将使用所选的选项插入表3。

菜肴1,2和& 3属于国家1,菜肴4,5和& 6属于国家2等。

我使用MySQLi Object Orientated来连接数据库。

两个选择查询和插入查询运行良好。我在第3张表中看到了选择。

荷兰语 - 英语
兰登 - 国家
gerechten - 菜肴

在第3个数据库表中,lan​​dGerecht的列是:
- landGerecht_id,(自动增量)
- land_id,
- gerecht_id,
- created_at(当前时间戳)

插入文件中的代码insertKeuze.php:

<?php
$landKeuze = $_POST['landen'];
$gerechtKeuze = $_POST['gerechten'];

if (!empty($landKeuze) && !empty($gerechtKeuze)) {
   // Insert statement with placeholders for the values to database table landGerecht
   $queryInsert = "INSERT INTO `lab_stage_danush` . `landGerecht` (`land_id`, `gerecht_id`) VALUES ( ?, ?)";

      // If query is prepared then bind $landKeuze with land_id and $gerechtKeuze with gerecht_id and execute the query.
      if ($stmt = $mysqli->prepare($queryInsert)) {
         // Protect and validate land against hacking
         $landKeuze = hack_filter(filter_var($_POST['landen'], FILTER_SANITIZE_NUMBER_INT));
        // Protect and validate gerecht against hacking
        $gerechtKeuze = hack_filter(filter_var($_POST['gerechten'], FILTER_SANITIZE_NUMBER_INT));

        $stmt->bind_param('ii', $landKeuze, $gerechtKeuze);
        // If statement is not executed then give an error message.

        if (!$stmt->execute()) {
            echo 'Failed to execute the query: ' . $stmt->error . ' in query: ' . $queryInsert;
        }
        $stmt->close();
      }
      // Else give an error message
      else {
         echo 'Something went wrong in the query: ' . $mysqli->error;
      }
}

// Function to controle the insert values from POST landen and POST gerechten
function hack_filter($dataHackControl) {
    $dataHackControl = trim($dataHackControl); // Strip whitespaces
    $dataHackControl = stripslashes($dataHackControl); // Strip off backslashes
    $dataHackControl = htmlspecialchars($dataHackControl); // Convert special characters to HTML entities
    return $dataHackControl;
}
?>

我用下面的代码解决了一个问题。我在bind_param()之后的代码中添加了它:

<?php 
if ($landKeuze == 0 || $landKeuze > 5 || $gerechtKeuze == 0 || $gerechtKeuze > 15) {
    echo '<script>alert("Deze keuze bestaat niet. Kies opnieuw!");location.replace("keuze.php")</script>';
    $landKeuze = null;
    $gerechtKeuze = null;
} 
?>

但问题是:
在源代码(F12)中,仍然可能会出现与所选国家/地区相关的错误菜肴。

例如:
国家(id)1是中国菜(id)1 Babi Pangang,
国家(id)2是斯里兰卡与菜(id)4鸡肉咖喱。

当我选择中国和Babi Pangang(1,1)并且我将菜肴值/ id改为4(鸡肉咖喱)然后我得到(1,4)在表中但不允许。< / p>

如果没有if-else if contruction,我怎么能防止这种情况?

提前谢谢!

0 个答案:

没有答案