-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_keys.php
More file actions
38 lines (32 loc) · 1.16 KB
/
add_keys.php
File metadata and controls
38 lines (32 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
function add_keys($key_length, $db, $table){
// create data to populate table
$consentkeys = range(10**($key_length-1), 10**($key_length)-1);
shuffle($consentkeys);
// don't need this piece below since we start at 100000 if key length is 6
/* for($i = 0; $i < count($consentkeys); $i++){
$consentkeys[$i] = str_pad(strval($consentkeys[$i]), $key_length, '0', STR_PAD_LEFT);
} */
// create table
$db->exec("CREATE TABLE IF NOT EXISTS ".$table." (
id INTEGER PRIMARY KEY AUTO_INCREMENT,
codekey VARCHAR(255),
consent TINYINT,
used TINYINT
)");
// start transaction
$db->exec("BEGIN;");
// add values
$insert = "INSERT INTO ".$table." (codekey, consent, used) VALUES (:key, floor(rand()*2), 0)";
$statement = $db->prepare($insert);
$statement->bindParam(':key', $key);
for($i = 0; $i < count($consentkeys); $i++){
$key = $consentkeys[$i];
$statement->execute();
}
$db->exec("COMMIT;");
$success_query = $db->query("SELECT COUNT(codekey) as count FROM ".$table.";");
$nrows = $success_query->fetch(PDO::FETCH_ASSOC)['count'];
return($nrows);
}
?>