Click here to Skip to main content
15,896,437 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys
I've wrote this function that returns a time(number )that must not exist in the same day in the database which means that the same time must not be repeated but when i execute it the time(number) is repeated
can you help me sort this one out?

note: the table is empty and it will be filled when the entire code is executed i have just posted the function that must not repeat the time.


function checkTime($className,$day,$conn){
      
    global $numberLimet,$Daily,$UserLimt,$exist;

    $sqlHoure = "SELECT * from schedule WHERE classname='".$className."'";
    $hourQuery = mysqli_query($conn,$sqlHoure);
    $h = array();
    

    while ($hour = mysqli_fetch_assoc($hourQuery)){
 
        
        $h[$hour['hour']] = $hour['day1'];

       // array_search();
    }

    $number = rand(1,7);

    if(array_search($day,$h) && array_key_exists($number,$h)){


        return 'error';


    }else{

        
     if($hour['hour'] != $number && $hour['day1']  != $day){

        $numberLimet++;
        if($numberLimet == $Daily){
            $UserLimt = 1;
            $FristAdd = false;

            echo("UserLimt" . $UserLimt);

            return 'errorLimit';
        }else{
            return $number;

        }

     }else{
     }
      
     // 
        

    }

    return $h;


          
}


What I have tried:

i tried to change the code many times but the same result
Posted
Updated 14-Dec-18 8:37am
v11

1 solution

PHP
$sqlHoure = "SELECT * from schedule WHERE classname='".$className."'";

Not necessary a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
Share this answer
 
Comments
RonoRed 14-Dec-18 14:16pm    
only the admin can insert data in the database so the class Name can be only inserted by the admin only
RonoRed 14-Dec-18 14:22pm    
to be honest i don't have experience i just want to know what is the problem with the random number
MadMyche 14-Dec-18 14:22pm    
So if the program is only used by an Admin, it is OK to use poor coding practices?
RonoRed 14-Dec-18 14:19pm    
and i used !preg_match("/^[a-zA-Z]*$/"
RonoRed 14-Dec-18 14:20pm    
but i need help with the random number :P

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900