everyone!
I am having trouble with creating a MySQL statement.
Example:
I have one column named 'keys', and another column named 'isUsed', and the 'keys' column consists of many different keys in this format: xxxx-xxxx-xxxx, and I want to check if that key has already been used or not.
This is the format of the table:
Key | isUsed
xxxx-xxxx-xxxx | 0
I have already made it so it checks if the key exists by using this code:
$arrExistKey = mysqli_query($resDBCon, "SELECT Key FROM keys WHERE Key = '$strKey'");
$intKeys = mysqli_num_rows($arrExistKey);
if ($intKeys != 1) {
sendError('The beta-key does not exist!');
}
So I have checked if the key exists, now I'm just trying to figure out how to check if it has already been used.
If anyone can help me out, that would be great! Thank you.
How about just encoding the logic in the SQL?
$arrExistKey = mysqli_query($resDBCon, "SELECT Key FROM keys WHERE Key = '$strKey' AND isused = 1");
Here is what you need!
<?php
$arrExistKey = mysqli_query($resDBCon, "SELECT Key FROM keys WHERE Key = '$strKey'");
$intKeys = mysqli_num_rows($arrExistKey);
$sql = mysqli_query($resDBCon, "SELECT * FROM keys WHERE Key = '$strKey'");
while ($row = mysql_fetch_array($sql)) {
$used = $row['isUsed'];
}
if ($intKeys != 1) {
sendError('The beta-key does not exist!');
} else {
if ($used == 1) {
echo "the key has already been used recently!";
} else {
//action
}
}
?>
Good luck!
One solution would be to modify your SQL statement to
"SELECT Key, isUsed FROM keys WHERE Key = '$strKey'"
Now you can check if the key exists using your solution, followed by the mysqli_fetch_array
function to retrieve the value for isUsed
.
Also please check if $strKey
does not contain illegal characters, before inserting it into the SQL statement. Depending on the rest of your code, it could be vulnerable to SQL injection.
Good luck with the implementation!
This may be what you need:
// SELECT THE KEY YOU NEED
$result = mysqli_query($resDBCon, "SELECT * FROM keys WHERE Key = '$strKey'");
// LOOP ALL RESULTS AND ENTERING IN THE WHILE MEANS THAT THE KEY EXISTS
while($row = mysqli_fetch_array($result) {
// HERE YOU CHECK IF KEY WAS USED
if($row['isUsed'] == 1)
echo 'Key was used';
}