I've been troubleshooting this for two days and am at a loss.
I have this code where a SELECT query is run in one table and then if the token matches the one in the db it runs a INSERT query on a different table.
The SELECT query works just fine however, no matter which way I execute the INSERT query it always throws back a db is locked error.
I have tried executing the INSERT query with
- exec()
- query()
- querySingle()
- prepare() / bindParam() / execute()
Understanding that it could also have not finished the SELECT query I have also tried
- setting the busyTimeout() time out to 5 seconds
- Adding a sleep() in between the SELECT and INSERT queries
- Closing and reopening the file
- Using object oriented and closing and reopening the file in each function
Code
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$db = new SQLite3('../grasscar_test.sqlite');
$token = $_POST['token'];
$res = $db->query("SELECT key FROM admin WHERE key = '$token'");
if ($res === FALSE){
echo '{"correct":"no"}';
}
while ($row = $res->fetchArray()){
$key = "{$row['key']}";
if ($key === $token){
//generate token
$cookieog = openssl_random_pseudo_bytes(32);
$cookie = bin2hex($cookieog);
//respond with token
echo '{"correct":"yes","token":"'.$cookie.'"}';
//get expiary time
$expiary = date("Y-m-d H:i:s", strtotime("+30 minutes"));
//add token and expiary date
$insert = $db->exec("INSERT INTO admin_cookie (cookie, expiary) VALUES ('$cookie', '$expiary')"); //This is the line throwing the error
print_r($insert);
}else{
echo '{"correct":"no"}';
}
}
$db->close();
?>