Susant
Susant

Reputation: 87

check duplicate rand function value in database and generate it again

i create rand funtion for generating random value and concatenate with other value and show in the text field through ajax before insert this value. but here how can i check this random generating value is exists or not in database before inserting this value in database.if value is exists then again generate rand function value and again concatenate this and show the value in textbox. how can i do this? my code is below 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();
    }
}
?>

Upvotes: 1

Views: 250

Answers (2)

swapnil kambe
swapnil kambe

Reputation: 123

when ever you save the data of the form store rand function value too means in second time you can retrieve the rand function value and compare with current rand function generating value.

Upvotes: 1

sensorario
sensorario

Reputation: 21668

I suggest to use md5 function and/or time() function such as:

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

Your updated code should be:

$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;

This solution provide unique value. You can use also uniqid() function. Also remember to set as unique the database field.


Another solution is to keep roll creation login in a function and create another function to check if the roll exists or not. Your responsibility to check if other rolls are store in the db or in a text file, ...

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();
    }
}

Upvotes: 1

Related Questions