doutuo1939 2012-03-23 15:19
浏览 315
已采纳

php将数据库中的值与文本框中的值进行比较

i have a multiple amount of text fields, the amount of text fields is due to how much data is in a database. The input for both are integers, all i want is when the values are inputted into the text fields it throws an error if the inputted data is larger than the value in the data base for example in a markscheme the data inputted into the textbox is the mark given to the student and the data in the database is the maxmark for that particular question, so therefore it cannot exceed that value so in effect i want to compare the values and if the text input value is larger than that of the one in the database it throws and error :)

  • 写回答

2条回答 默认 最新

  • dongmo3413 2012-03-23 16:01
    关注

    If it's OK for you to rely on your users having javascript enabled, I would say the easiest is to verify the data on the client side.

    You could do something like this:

    <html>
    <head>
      <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    </head>
    
    <body>
      <input type="text" value="5" name="grade[123]" data-max="10" />
    
      <script type="text/javascript">
        $(function() {
          $('input[type="text"]').blur(function(e) {
            var $this = $(this);
            var max = parseInt($this.data('max'), 10);
    
            if (parseInt($this.val(), 10) > max) {
              var name = $this.attr('name');
              console.error('Value ' + $this.val() + ' in field "' + name + '" exceed its maximum value of ' + max);
              $this.focus();
            }
          })
        });
      </script>
    </body>
    </html>
    

    Or you could replace all this logic with simple HTML5 number fields:

    <input type="number" value="5" name="grade[123]" min="0" max="10" />
    

    Obviously, one should never trust their users. You should always double-check the data on the server side and notify users about the possible errors.

    This is something you could do:

    <?php
    
    if (!empty($_POST)) {
        // ...
        // fetch max values from the database in the form of 
        // array(
        //     id => [max],
        // );
        $maxValues = array( /* ... */ );
    
        // prepare some error array if you want to show errors next to individual fields
        $errors = array();
    
        // ...and then loop through the posted array
        foreach ($_POST['grades'] as $id => $value) {
            // make sure the submitted value is an integer
            if (!ctype_digit($value) && !is_int($value)) {
                $errors[$id] = 'Invalid value';
                continue;
            }
    
            if ((int) $value > (int) $maxValues[$id]) {
                $errors[$id] = 'Value cannot be more than ' . $maxValues[$id];
            }
        }
    
        // assign errors to the view or do whatever is required in your script
        // ...
    }
    

    It shouldn't be difficult to understand what I was doing there. Basically, have one reference array and the data array to verify against (note: your HMTL field names must have the square brackets in them to act as arrays). And then just loop through the submitted data and verify against the reference array.

    Just like Ryan Kempt said, there are lots of ways you could do it and without a specific example of your data structure or how you want the errors/exceptions to be presented to the user, it's quite difficult to write you an exact code.

    Nevertheless, have a look at our suggestions and start from there. And best of luck!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog