检查数据库中的重复rand函数值并再次生成它

时间:2017-06-06 04:40:59

标签: javascript php jquery mysql ajax

我创建rand函数用于生成随机值并与其他值连接,并在插入此值之前通过ajax显示在文本字段中。但是在这里我如何在数据库中插入此值之前检查此随机生成值是否存在于数据库中。如果值存在则再次生成rand函数值并再次连接并在文本框中显示该值。我怎样才能做到这一点?我的代码如下     的index.php

    <html>    
<head>
    <title>Untitled Document</title>
    <script 
    src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
    </script>
    <script>
    $( document ).ready(function() {});
    function my_validate_func() {
    var name = $('#name').val();
    var year = $('#year').val();
    var course = $('#course').val();
    var branch_name = $('#branch_name').val();
        if ($('#name').val() != "" && $('#year').val() != "" &&
            $('#course').val() != "" && $('#branch_name').val() != "") {
            $.ajax({
                type: "POST",
                url: 'roll.php',
                data: { name: name, year: year, branch_name: branch_name, course: course },
                success: function(response) {
                    $('#roll').val(response);
                }
            });
        }
    }
</script>
</head>

<body>
    <form method="post" action="">
        <input type="text" name="name" id="name" onChange="my_validate_func()">
        <input type="text" name="phone" id="phone" onChange="my_validate_func()">
        <input type="text" name="course" id="course" onChange="my_validate_func()">
        <input type="text" name="center" id="center" onChange="my_validate_func()">
        <input type="text" name="roll" id="roll" value="">
    </form>
</body>

</html>

roll.php





    <?php
function calculateRoll()
{
    $name1        = $_POST['name'];
    $year1        = $_POST['year'];
    $course1      = $_POST['course'];
    $branch_name1 = $_POST['branch_name'];
    $name2        = substr($name1,0,3);
    $name         = strtoupper($name2);
    $year         = substr($year1,-2);
    $branch_name  = strtoupper(substr($branch_name1,0,3));
    $course2      = substr($course1,0,3);
    $course       = strtoupper($course2);
    $rand         = rand(100000,999999);
    $roll         =$branch_name.$name.$course.$year.$rand;
    //return $roll;
    echo $roll;
}

function isValidRoll($roll) {
    mysql_connect("localhost","root","");
    mysql_select_db("sigma");
    $sql="SELECT count(*) as total FROM student WHERE roll = '$roll'";
    $result = mysql_query($sql);

    $data = mysql_fetch_assoc($result);
    return $data['total'] == 0;
}

$validRoll = false;
$roll = calculateRoll();
while (!$validRoll) {
    if (isValidRoll($roll)) {
        $validRoll = true;
    } else {
        $roll = calculateRoll();
    }
}
?>

2 个答案:

答案 0 :(得分:1)

我建议使用md5函数和/或time()函数,例如:

$rand         = md5(time() + rand(100000,999999));

您的更新代码应为:

$name1        = $_POST['name'];
$year1        = $_POST['year'];
$course1      = $_POST['course'];
$branch_name1 = $_POST['branch_name'];
$name2        = substr($name1,0,3);
$name         = strtoupper($name2);
$year         = substr($year1,-2);
$branch_name  = strtoupper(substr($branch_name1,0,3));
$course2      = substr($course1,0,3);
$course       = strtoupper($course2);
$rand         = md5(time() + rand(100000,999999));
$roll         = $branch_name.$name.$course.$year.$rand;

echo $roll;

此解决方案提供独特的价值。您也可以使用uniqid()功能。还记得将数据库字段设置为唯一。

另一种解决方案是在一个函数中保持卷创建登录,并创建另一个函数来检查卷是否存在。您有责任检查其他卷是否存储在数据库或文本文件中,...

function calculateRoll()
{
    $name1        = $_POST['name'];
    $year1        = $_POST['year'];
    $course1      = $_POST['course'];
    $branch_name1 = $_POST['branch_name'];
    $name2        = substr($name1,0,3);
    $name         = strtoupper($name2);
    $year         = substr($year1,-2);
    $branch_name  = strtoupper(substr($branch_name1,0,3));
    $course2      = substr($course1,0,3);
    $course       = strtoupper($course2);
    $rand         = rand(100000,999999);
    return $branch_name.$name.$course.$year.$rand;
}

function isValidRoll($roll) {
    $result = mysql_query("SELECT count(*) as total FROM student WHERE roll = '$roll'")
        or die("Query not valid: " . mysql_error());
    $data = mysql_fetch_assoc($result);
    return $data['total'] == 0;
}

$validRoll = false;
$roll = calculateRoll();
while (!$validRoll) {
    if (isValidRoll($roll)) {
        $validRoll = true;
    } else {
        $roll = calculateRoll();
    }
}

答案 1 :(得分:1)

当你保存表单存储rand函数值的数据时也意味着在第二次你可以检索rand函数值并与当前rand函数生成值进行比较。