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

我有多个文本字段,文本字段的数量是由于数据库中有多少数据 。 两者的输入都是整数,我想要的只是当值输入到文本字段时,如果输入的数据大于数据库中的值,则会抛出错误
例如

输入到数据库中的数据 textbox是给学生的标记,数据库中的数据是该特定问题的最大标记,因此它不能超过该值
实际上我想比较这些值,如果文本输入值大于 它抛出的数据库中的那个和错误:)</ p>
</ div>

展开原文

原文

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 :)

dougaxing8673
dougaxing8673 向我们展示一些代码,交配
8 年多之前 回复

2个回答

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!



很多方法可以解决这个问题,但几乎所有的方法都可以使用javascript解决方案进行客户端检查,PHP用于服务器端 ...对于前端,您可以查询数据库并在隐藏输入中输出信息,然后使用javascript解决方案将文本框的值与匹配的隐藏div的值进行比较。</ p>

当文本框失去焦点时,您可以使用AJAX并根据数据库查询文本框中的内容。</ p>

当用户最终提交表单时,您还应该使用daabase再次验证数字 PHP。</ p>

所以...需要更多信息,但上述步骤是你想要做的 - 你如何做到这些取决于你,如果你需要特定的帮助 在尝试上述方法并遇到问题后,我们很乐意为您提供更多帮助。</ p>

编码愉快,祝您好运!</ p>
</ div>

展开原文

原文

Lots of ways to tackle this but pretty much with everything you can use a javascript solution for client-side checking and PHP for server-side... for front-end you could query the DB and output the information in hidden inputs and then compare the value of the textbox to the value of the matching hidden div using a javascript solution.

When the textbox loses focus you could use AJAX and query what's in the textbox against your database.

When the user submits the form finally you should also then verify the numbers again against the daabase using PHP.

So... would need more information but the above steps are what you're going to want to do - how you do them is up to you and if you need specific help after trying the above methods and running into issues, we'd love to help you more.

Happy coding and best of luck!

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问