setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Check if the users table exists already $sqlCheckAdminUserTable = $conn->prepare("SHOW TABLES LIKE '" . $adminUserTableName . "'"); // Run the query $sqlCheckAdminUserTable->execute(); //Check if any rows exist - if not, create the table $adminCount = $sqlCheckAdminUserTable->rowCount(); if ($adminCount == 0) { echo "

Admins table not found! This is probably initial setup.

Creating safe admins table...

"; try { $conn->query($sqlCreateAdminTable); echo "

Table '" . $adminUserTableName . "' successfully created (safe admins)

"; } catch (PDOException $e) { echo $sqlCreateUserTable . "
" . $e->getMessage(); } } // Variables for the various input fields $username = $_POST["username"]; $password = password_hash($_POST["password"], PASSWORD_DEFAULT); // Hash the password for security $twitch = $_POST["twitch"]; $discord = $_POST["discord"]; $discordLink = $_POST["discordLink"]; $youtube = $_POST["youtube"]; $youtubeLink = $_POST["youtubeLink"]; // Gotta check and make sure the user we're creating is an admin $privileges = 0; if (filter_has_var(INPUT_POST, "privileges")) { $privileges = 1; } // Prepare the query $insert = $conn->prepare("INSERT INTO " . $adminUserTableName . " (username, password, discord, discordLink, twitch, youtube, youtubeLink, privileges) VALUES (:username, :password, :discord, :discordLink, :twitch, :youtube, :youtubeLink, :privileges)"); // Bind parameters to the query $insert->bindParam(":username", $username); $insert->bindParam(":password", $password); $insert->bindParam(":discord", $discord); $insert->bindParam(":discordLink", $discordLink); $insert->bindParam(":twitch", $twitch); $insert->bindParam(":youtube", $youtube); $insert->bindParam(":youtubeLink", $youtubeLink); $insert->bindParam(":privileges", $privileges); // Execute $insert->execute(); // Check if users table exists, if not run the initialize script, otherwise just make the user $sqlCheckUserTable = $conn->prepare("SHOW TABLES LIKE " . $userTableName); // Run the query, if the table doesn't exist, initialize the database first if ($sqlCheckUserTable !== false && $sqlCheckUserTable->rowCount() > 0) { echo "

Users table found

"; // Now add them to the regular users table as well // Prepare the query $insert = $conn->prepare("INSERT INTO " . $userTableName . " (username, password, discord, discordLink, twitch, youtube, youtubeLink, privileges) VALUES (:username, :password, :discord, :discordLink, :twitch, :youtube, :youtubeLink, :privileges)"); // Bind parameters to the query $insert->bindParam(":username", $username); $insert->bindParam(":password", $password); $insert->bindParam(":discord", $discord); $insert->bindParam(":discordLink", $discordLink); $insert->bindParam(":twitch", $twitch); $insert->bindParam(":youtube", $youtube); $insert->bindParam(":youtubeLink", $youtubeLink); $insert->bindParam(":privileges", $privileges); // Execute $insert->execute(); } else { echo "

Users table not found! This is probably (still) initial setup. Creating...

"; initialiseDatabase(); // Next we're going to copy any safe admins into the users table. // This will make userlists easier to work with //echo "

Copying users from safe admins...

"; //$copyAdmins = $conn->prepare("INSERT INTO " . $userTableName . " SELECT * FROM " . $adminUserTableName); //$copyAdmins->execute(); //echo "

Copied!

"; } if ($userCount == 0) { } else { } echo "Safe Admin created successfully!"; } catch (PDOException $e) { // failed connection echo "Connection failed: " . $e->getMessage(); } $conn = null; ?>