I have a problem. Namely, I want a very simple form with select fields to be entered into a database. For this I use MYSQLi. Here you can see the code I use in HTML. But should be correct so far.
<form action="./php-functions/apply-server.php" method="post" class="form-group">
<div class="mb-3">
<label class="form-label">Servertyp</label>
<select class="custom-select" name="applyservertype">
<option value="1">Gameserver</option>
<option value="2">vServer</option>
<option value="" disabled selected>Bitte auswählen...</option>
</select>
</div>
<div class="mb-3" id="apply-vserver-operatingsystem">
<label class="form-label">Betriebssystem</label>
<select class="custom-select" name="serveroperatingsystem">
<option value="u1604">Ubuntu 16.04</option>
<option value="u1804">Ubuntu 18.04</option>
<option value="u1810">Ubuntu 18.10</option>
<option value="u1904">Ubuntu 19.04</option>
<option value="u2004">Ubuntu 20.04</option>
<option value="d10">Debian 10</option>
<option value="d9">Debian 9</option>
<option value="" disabled selected>Bitte auswählen...</option>
</select>
</div>
<div class="mb-3" id="apply-vserver-system">
<label class="form-label">Systemeigenschaften</label>
<select class="custom-select" name="serversystem">
<option value="1220">1 Kerne | 2GB RAM | 20GB Speicher</option>
<option value="2440">2 Kerne | 4GB RAM | 40GB Speicher</option>
<option value="3660">3 Kerne | 6GB RAM | 60GB Speicher</option>
<option value="4880">4 Kerne | 8GB RAM | 80GB Speicher</option>
<option value="" disabled selected>Bitte auswählen...</option>
</select>
</div>
<div class="mb-3" id="apply-server-game">
<label class="form-label">Spiel</label>
<select class="custom-select" name="servergame">
<option value="ark">ARK: Survival Evolved</option>
<option value="gmod">Garry's Mod</option>
<option value="mc">Minecraft Paper</option>
<option value="rust">Rust</option>
<option value="csgo">CS:GO</option>
<option value="" disabled selected>Bitte auswählen...</option>
</select>
</div>
<div class="mb-3" id="applyservergamesystem">
<label class="form-label">Systemeigenschaften</label>
<select class="custom-select" name="servergamesystem">
<option value="4450">4 Kerne | 4GB RAM | 5GB Speicher</option>
<option value="" disabled selected>Bitte auswählen...</option>
</select>
</div>
<div class="mb-4">
<label class="form-label">Beschreibung</label>
<textarea class="form-control" rows="5" min="50" max="1500" id="description" name="description" placeholder="• Wozu benötigst du den Server? (Konzept) • Existiert bereits eine Community? • Existiert bereits eine Website? • ..." required></textarea>
<small class="form-text">Bitte <b>möglichst ausführlich</b>, um eine schnelle und gezielte Abwicklung zu garantieren.<br>Mindestens 50 Zeichen, maximal 1500 Zeichen.</small>
</div>
<button type="submit" class="btn btn-secondary">Absenden <i class="far fa-paper-plane"></i></button>
</form>
Now follows my PHP code. Here I use the switch case, because I have two select cases and want to enter each differently into the database.
<?php
session_start();
if (!isset($_SESSION['loggedin'])) {
$_SESSION['notification'] = "not-loggedin";
header('Location: ../../index.php');
exit;
}
$DATABASE_HOST = '#';
$DATABASE_USER = '#';
$DATABASE_PASS = '#';
$DATABASE_NAME = '#';
$con = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS, $DATABASE_NAME);
if (mysqli_connect_errno()) {
exit('Fehler bei der Verbindung zu MySQL: ' . mysqli_connect_error());
}
$type = intval($_POST['applyservertype']);
$none = 'none';
switch ($type) {
case 1:
if ($stmt = $con->prepare('INSERT INTO serverapply (username, servertype, operatingsystem, game, system, serverdescription) VALUES (?, ?, ?, ?, ?, ?)')) {
$stmt->bind_param('ssssss', $_SESSION['name'], $_POST['applyservertype'], $none, $_POST['servergame'], $_POST['servergamesystem'], $_POST['description']);
$stmt->execute();
$_SESSION['notification'] = "server-successful";
header('Location: ' . $_SERVER['HTTP_REFERER']);
exit;
} else {
$_SESSION['notification'] = "server-error";
header('Location: ' . $_SERVER['HTTP_REFERER']);
exit;
}
$stmt->close();
break;
case 2:
if ($stmt = $con->prepare('INSERT INTO serverapply (username, servertype, operatingsystem, game, system, serverdescription) VALUES (?, ?, ?, ?, ?, ?)')) {
$stmt->bind_param('ssssss', $_SESSION['name'], $_POST['applyservertype'], $_POST['serveroperatingsystem'], $none, $_POST['serversystem'], $_POST['description']);
$stmt->execute();
$_SESSION['notification'] = "server-successful";
header('Location: ' . $_SERVER['HTTP_REFERER']);
exit;
} else {
$_SESSION['notification'] = "server-error";
header('Location: ' . $_SERVER['HTTP_REFERER']);
exit;
}
break;
default:
$_SESSION['notification'] = "server-error";
header('Location: ' . $_SERVER['HTTP_REFERER']);
break;
}
Here is the picture of my database structure.
The PHP code is not executing properly for some reason I don't see. The form will be submitted and redirected to the page as if the submission was successful. After submitting, however, no database entry is created.