I'm allowing the logged in user to change the rsvp status via a dropdown on their profile page. When they submit the change, it's taking a while to update the field and sometimes times out. I suspect that I'm causing this with the way it's coded to do the database update but can't figure it out.
<?php
include_once 'header.php';
require_once 'includes/dbh.inc.php';
require_once 'includes/functions.inc.php';
if(isset($_SESSION["emailAddress"])) {
$sql = "SELECT * FROM users WHERE email='$_SESSION[emailAddress]'";
$stmt = mysqli_prepare($conn, $sql);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$row = mysqli_fetch_assoc($result);
mysqli_stmt_close($stmt);
$inGet = "SELECT * FROM users WHERE rsvp='in';";
$inData = mysqli_query($conn, $inGet);
$inTotal = mysqli_num_rows($inData);
if(isset($_POST['apply'])) {
$rsvp = $_POST['status'];
$email = $_SESSION['emailAddress'];
$firstName = $row['firstName'];
$lastName = $row['lastName'];
do {
$sql2 = "UPDATE users SET rsvp='$rsvp' WHERE email='$_SESSION[emailAddress]';";
$stmt2 = mysqli_prepare($conn, $sql2);
mysqli_stmt_execute($stmt2);
mysqli_stmt_close($stmt2);
} while ($inTotal <= 8);
if (($inTotal == 9 && $rsvp == "in")) {
$sql3 = "UPDATE users SET rsvp='waitlist' WHERE email='$_SESSION[emailAddress]';";
$stmt3 = mysqli_prepare($conn, $sql3);
mysqli_stmt_execute($stmt3);
mysqli_stmt_close($stmt3);
header("Location: dashboard.php");
exit();
}
}
}
?>
I've tried to call and close statements to avoid multiple statements being open at the same time.
I'm expecting the changes to be rather instant in the update of the database to reflect on user's profile and the main dashboard.