setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Grab session username to make sure we're updating the person logged in
$username = $_SESSION["username"];
// Grab the existing data, so we can only update the things that got updated
$sqlGetUserInfo = $conn->prepare("SELECT * FROM " . $userTableName . " WHERE username=\"" . $username . "\"");
$sqlGetUserInfo->execute();
$userInfo = $sqlGetUserInfo->fetch(); // fetch row
// These IF blocks check if the data entered is different from the data already in the DB
// If the information is the same then we copy the stuff over, otherwise write it
if ($_POST["twitch"] != $userInfo["twitch"] && $_POST["twitch"] != "") {
$twitch = $_POST["twitch"];
} else {
$twitch = $userInfo["twitch"];
}
echo $twitch;
echo "";
if ($_POST["youtube"] != $userInfo["youtube"] && $_POST["youtube"] != "") {
$youtube = $_POST["youtube"];
} else {
$youtube = $userInfo["youtube"];
}
echo $youtube;
echo "";
if ($_POST["youtubeLink"] != $userInfo["youtubeLink"] && $_POST["youtubeLink"] != "") {
$youtubeLink = $_POST["youtubeLink"];
} else {
$youtubeLink = $userInfo["youtubeLink"];
}
echo $youtubeLink;
echo "";
if ($_POST["discord"] != $userInfo["discord"] && $_POST["discord"] != "") {
$discord = $_POST["discord"];
} else {
$discord = $userInfo["discord"];
}
echo $discord;
echo "";
if ($_POST["discordLink"] != $userInfo["discordLink"] && $_POST["discordLink"] != "") {
$discordLink = $_POST["discordLink"];
} else {
$discordLink = $userInfo["discordLink"];
}
echo $discordLink;
echo "";
// Prepare the command
$update = $conn->prepare("UPDATE " . $userTableName . " SET
twitch = :twitch,
youtube = :youtube,
youtubeLink = :youtubeLink,
discord = :discord,
discordLink = :discordLink
WHERE username = :username
");
// Bind parameters to query
$update->bindParam(":username", $username);
$update->bindParam(":twitch", $twitch);
$update->bindParam(":youtube", $youtube);
$update->bindParam(":youtubeLink", $youtubeLink);
$update->bindParam(":discord", $discord);
$update->bindParam(":discordLink", $discordLink);
$update->execute(); // Execute query
// Function from StackOverflow used to get the base URL, to which we append
// the redirect (where the user came from)
function url(){
return sprintf(
"%s://%s/user/%s",
isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http',
$_SERVER['SERVER_NAME'],
$_SESSION["username"]
);
}
$address = url();
// Redirect user back to their page
echo "";
echo "Account successfully updated
";
echo "You should have been redirected to your account. Here's a link:
";
echo "My Account
";
} catch (PDOException $e) { // failed connection
echo "Connection failed: " . $e->getMessage();
}
$conn = null;
?>