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']);
$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.

My database version

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.

Luca
  • 1
  • Did you try to display error message? Did you try to call this statement (with resolved data) in any DB client? For an instance, in Maria DB the `system` may be a keyword (depending on version) and should be changed or quoted. BTW we're programmers, we can read. Please read about [why you shouldn’t upload images of code or errors when asking a question](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question/285557#285557) – biesior Apr 18 '21 at 16:12
  • This is exactly the same as https://stackoverflow.com/questions/67148914/mysqli-php-code-does-not-execute-properly Why did you repost it? – Dharman Apr 18 '21 at 17:36
  • What did you debug so far? Which part of the code is executed? You need to give us some more information. – Dharman Apr 18 '21 at 17:39
  • Enable mysqli error reporting and then check the error logs [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) – Dharman Apr 18 '21 at 17:41

1 Answers1

-1

You are inserting an extra value ssssss. Just match the values with the attributes. The values are 7 and the attributes are 6.

bind_param should be like:

$stmt->bind_param($_SESSION['name'], $_POST['applyservertype'], $none, $_POST['servergame'], $_POST['servergamesystem'], $_POST['description']);

shahidiqbal
  • 375
  • 1
  • 7