I have a little situation here. If in my database in my “Session” Table if I have the data below:
SessionId
C
Then what happens with my code is that it would not display the string above when user generates a new string as that it is already in the database. This works fine for example above.
The problem I have though is that if I have multiple sessions(exams), then in the database below it will display the strings as below in the database:
SessionId
C1
C2
C3
The above is for 3 exams. But the problem is that the string “C” can still be generated in the code below. What my question is that if there is a number at the end of the string like the table above, then how can I not generate the string as it is still in the database? The strings will always be 1 character long:
function id_generator(){
$id = "";
$a = array( "A" , "B" , "C" , "D" , "E" , "F" , "G" , "H" , "I" , "J" , "K" , "L" , "M" , "N" , "O" , "P" , "Q" , "R" , "S" , "T" , "U" , "V" , "W" , "X" , "Y" , "Z" );
for( $i = 0 ; $i < 1 ; $i++ ){ $r = rand( 0 , 25 );
$id .= $a[ $r ];
}
return $id;
};
$is_there = true;
while( $is_there ){
$id = id_generator(); // your function to generate id;
$result = "SELECT SessionId FROM Session WHERE SessionId LIKE ?";
$stmt=$mysqli->prepare($result);
$id = $id . '%';
$stmt->bind_param("s",$id);
$stmt->execute();
// get result and assign variables (prefix with db)
$stmt->bind_result($dbSessionId);
$stmt->store_result();
$stmtnum = $stmt->num_rows();
if($stmtnum == 0) {
$is_there = false;
}
}
....
</body>
<form>
<p><strong>1: Your Assessment ID: </strong><span id="idFont"><?php echo $id; ?></span></p>
....
why not use
like?