MySQL Sometimes Inserts Twice (PHP Script)

Viewed 34

I'm troubleshooting a save function, and sometimes it will save twice. Others, only once. If I refresh multiple times, it will submit only a single time. It happens if I have stepped away for a few minutes. The sessions are still valid, though. I'm trying to see if it is an issue with the code that I have written so far. I believe this page is enough to give the context.

## Requirements ##
require __DIR__ . '/../../config/account.php';
$dirOffset = "/../..";

# Verify Session and Role Check
check_loggedin($con, true, 'tripbuild.php');
require __DIR__ . $dirOffset . '/src/inc/rolecheck.php';

# Includes
require __DIR__ . $dirOffset . '/src/inc/functions.space-cruise.php';
    
# Check Session Data
if (!isset($_SESSION['destinationList'])) {
    $_SESSION['alertMessage'] = "No session detected: why not start a new trip?";
    $_SESSION['alertType'] = "caution";
    header('Location: index.php');
    exit;
}

if (isset($_GET['mode'])) {
    $mode = htmlspecialchars($_GET['mode']);
} else {
    $mode = "";
}

# Creation Time Stamp
$creation = time();

if ($mode != "replace") {
    
    # Insert Primary Trip
    $stmt = $con->prepare('INSERT INTO trips (creator, creation_ts ... cruise_status) VALUES (?, ? ... ?)');
    $stmt->bind_param('iisiiiissisisisisisisi', $_SESSION['id'], $creation ... $_SESSION["cruiseStatus"]);
    $stmt->execute();
    $tripid = $stmt->insert_id;
    $stmt->close();
    
    $_SESSION['tripid'] = $tripid;
    
    # Process Legs
    require __DIR__ . $dirOffset . '/src/inc/save.legs.php';
    
    # Back to Trip Builder
    $_SESSION['alertMessage'] = "Your trip was saved successfully!";
    $_SESSION['alertType'] = "success";
    header('Location: tripbuild.php');
    exit;

} else {
    
    if (!isset($_SESSION['tripid'])) {
        header('Location: savetrip.php');
        exit;
    }
    
    # Search for Existing Trip
    $stmt = $con->prepare('SELECT tripid FROM trips WHERE tripid = ?');
    $stmt->bind_param('i', $_SESSION['tripid']);
    $stmt->execute();
    $stmt->store_result();
    $stmt->bind_result($tripid);
    $stmt->fetch();
    $stmt->close();
    
    if ($tripid != $_SESSION['tripid']) {
        header('Location: savetrip.php');
        exit;
    }
    
    # Delete Existing Legs
    $stmt = $con->prepare('DELETE from trips_legs WHERE tripid = ?');
    $stmt->bind_param('i', $tripid);
    $stmt->execute();
    $stmt->close();
    
    # Update
    $stmt = $con->prepare('UPDATE trips SET route = ?, trip_start = ? ... WHERE tripid = ?');
    $stmt->bind_param('siiisssisisisisisisii', $_SESSION['destinationList'], $_SESSION['tripStartUnix'] ... $tripid);
    $stmt->execute();
    $stmt->close();
    
    # Process Legs
    require __DIR__ . $dirOffset . '/src/inc/save.legs.php';
    
    # Back to Trip Builder
    $_SESSION['alertMessage'] = "Your trip was updated successfully!";
    $_SESSION['alertType'] = "success";
    header('Location: tripbuild.php');
    exit;

}

Thank you!

0 Answers
Related