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 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料