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 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办