0

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="&#8226; Wozu benötigst du den Server? (Konzept)&#10;&#8226; Existiert bereits eine Community?&#10;&#8226; Existiert bereits eine Website?&#10;&#8226; ..." 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']);

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;
}

Database Table Structure database structure

The PHP code is not executing properly for some reason I don't see. My Opera browser always says "This page is not working" -> Error 500. Maybe you can help me and point out my mistake.

BigFatBaby
  • 1,525
  • 9
  • 19
Luca
  • 1
  • Look at your webserver error logs. – Syscall Apr 18 '21 at 13:13
  • PHP Fatal error: Uncaught Error: Cannot pass parameter 4 by reference in /var/www/html/userpanel/php-functions/apply-server.php:22\nStack trace:\n#0 {main}\n thrown in /var/www/html/userpanel/php-functions/apply-server.php on line 22, referer: http://###/userpanel/apply-server.php – Luca Apr 18 '21 at 13:16
  • `$none = 'none';` then use `$none` in `bind_param()`. _"Cannot pass parameter 4 by reference"_. A constant string cannot be passed by reference, but you can pass a variable. – Syscall Apr 18 '21 at 13:18
  • Thanks already. Now the redirection back to the page works. But still nothing is entered into the database. I have already made a ticket system with the same database. There everything works perfectly. So it is not because of the database. So it must still be the table or PHP code. – Luca Apr 18 '21 at 13:23
  • You should use `$stmt->error` if `execute()` returns `false`. (https://www.php.net/manual/en/mysqli-stmt.error.php) – Syscall Apr 18 '21 at 13:26
  • I am forwarded as if the entry was successful. `execute()` seems to output `true`. But the database entry does not appear in the database. – Luca Apr 18 '21 at 13:31
  • What is your DB and version? – Paul T. Apr 18 '21 at 14:26
  • 10.3.27-MariaDB – Luca Apr 18 '21 at 15:57

0 Answers0