Christoph C.
Christoph C.

Reputation: 938

PHP Form to Database - Not working

Once again I have a form that I want to submit to my database, but it is not working and I really have no idea why. I tested everything, but I can't see any error in my code.

Here is the code of my form:

<!-- Begin # Registration Form -->
<form method="post" action="register_teacher.php" id="registration-teacher-form" class="form-horizontal">
<div class="col-sm-12">
    <p>Willkommen zu unserer <strong>Online - Essensreservierung</strong> für Lehrer. Bitte geben Sie im nachfolgenden Schritt Ihre Daten bekannt. Die Wahl des Menüs, sowie die Essenstage, können Sie im nächsten Schritt angeben.</p><br>
    </div>
    <div class="form-group">
        <label for="inputEmail3" class="col-sm-2 control-label">Schule:</label>
        <div class="col-sm-10">
        <select class="form-control" name="schul" id="schul">
        <option value="no_selected" checked>Bitte auswählen</option>
        <option value="AHS Waldschule 1">AHS Waldschule 1</option>
        <option value="AHS Waldschule 2">AHS Waldschule 2</option>
        <option value="AHS Waldschule 3">AHS Waldschule 3</option>
    </select>
        </div>
    </div>
    <hr>
    <div class="form-group">
        <label for="inputEmail" class="col-sm-2 control-label">Email:</label>
        <div class="col-sm-10">
        <input type="email" class="form-control" name="emai" id="inputEmail" placeholder="Ihre E-Mail Adresse">
        </div>
    </div>
    <div class="form-group">
        <label for="inputPassword" class="col-sm-2 control-label">Passwort:</label>
        <div class="col-sm-10">
        <input type="password" class="form-control" name="passwor" id="inputPassword" placeholder="Ihr Passwort">
        </div>
    </div>
    <hr>
    <div class="form-group">
        <label for="inputEmail3" class="col-sm-2 control-label">Anrede:</label>
        <div class="col-sm-10">
        <select class="form-control" name="anred">
            <option value="Herr">Herr</option>
            <option value="Herr">Frau</option>
        </select>
        </div>
    </div>
    <div class="form-group">
        <label for="inputVorname" class="col-sm-2 control-label">Vorname:</label>
        <div class="col-sm-10">
        <input type="text" class="form-control" name="vornam" id="inputVorname" placeholder="Ihr Vorname">
        </div>
    </div>
    <div class="form-group">
        <label for="inputNachname" class="col-sm-2 control-label">Nachname:</label>
        <div class="col-sm-10">
        <input type="text" class="form-control" name="nachnam" id="inputNachname" placeholder="Ihr Nachname">
        </div>
    </div>
    <div class="form-group">
        <label for="inputStrasse" class="col-sm-2 control-label">Strasse:</label>
        <div class="col-sm-10">
        <input type="text" class="form-control" name="strass" id="inputStrasse" placeholder="Ihre Adresse">
        </div>
    </div>
    <div class="form-group">
        <label for="inputPLZ" class="col-sm-2 control-label">PLZ:</label>
        <div class="col-sm-10">
        <input type="text" class="form-control" name="pl" id="inputPLZ" placeholder="Ihre Postleitzahl">
        </div>
    </div>
    <div class="form-group">
        <label for="inputOrt" class="col-sm-2 control-label">Ort:</label>
        <div class="col-sm-10">
        <input type="text" class="form-control" name="or" id="inputOrt" placeholder="Ihr Wohnort">
        </div>
    </div>
    <div class="form-group">
        <label for="inputTelefon" class="col-sm-2 control-label">Telefon:</label>
    <div class="col-sm-10">
        <input type="tel" class="form-control" name="telefo" id="inputTelefon" placeholder="Ihre Telefonnummer">
    </div>
    </div>
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
        <p>Nachdem Sie die Registrierung abgesendet haben, erhalten Sie eine E-Mail mit einem Bestätigungslink. Sobald Sie Ihre E-Mail Adresse bestätigt haben, folgen Sie dem Link in der E-Mail um die Menüanmeldung durchführen zu können.
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            <button type="submit" name="submit1" value="submit" class="btn btn-custom pull-right">Send</button>
        </div>
    </div>
    </form>
<!-- End # Registration Form -->

Here is the code of my register_teacher.php:

    <?php
    // Create connection credentials
    $db_host = 'localhost';
    $db_name = 'DBNAME';
    $db_user = 'DBUSER';
    $db_pass = 'DBPASS';

// Create mysqli object
$connection = new mysqli ($db_host, $db_user, $db_pass, $db_name);

// Error Handler
if ($connection->connect_error) {
    printf ("Connection failed: %s\n", $connection->connect_error);
    exit();
}   
?>

<?php


// Check if form is submitted
if (isset ($_POST['submit1'])) {
    $schul = mysqli_real_escape_string ($connection, $_POST['schul']);
    $emai = mysqli_real_escape_string ($connection, $_POST['emai']);
    $passwor = mysqli_real_escape_string ($connection, $_POST['passwor']);
    $anred = mysqli_real_escape_string ($connection, $_POST['anred']);
    $vornam = mysqli_real_escape_string ($connection, $_POST['vornam']);
    $nachnam = mysqli_real_escape_string ($connection, $_POST['nachnam']);
    $strass = mysqli_real_escape_string ($connection, $_POST['strass']);
    $pl = mysqli_real_escape_string ($connection, $_POST['pl']);
    $or = mysqli_real_escape_string ($connection, $_POST['or']);
    $telefo = mysqli_real_escape_string ($connection, $_POST['telefo']);




    // Validate Input

$sql="INSERT INTO `teacher` (`schule`, `email`, `password`, `anrede`, `firstname`, `lastname`, `street`, `plz`, `city`, `phonenumber`)
VALUES ('$schul', '$emai', '$passwor' '$anred', '$vornam', '$nachnam', '$strass', '$pl', '$or', '$telefo')";



$query1 = mysqli_query($connection, $sql );
if(!mysqli_query($connection, $query1) ) {
    exit( header("Location: /teachercp/index.php") );
}


}
?>

MySQL connection is working. I also do not get any error message. Every time when I click on "Register", I am forwarded to a blank page on register_teacher.php! The redirection to teachercp/index.php is not working and no data is inside my database.

Any suggestions?

Thanks, Chris

Upvotes: 1

Views: 60

Answers (3)

Morshedul Arefin
Morshedul Arefin

Reputation: 1538

I tested your code in local server. It shows this error:

Warning: mysqli_error() expects exactly 1 parameter, 0 given in D:\xampp\htdocs\111\register_teacher.php on line 39

What I got error into your code is:

You misses a comma on line 39. See my image where you missed it. enter image description here

Upvotes: 1

Professor Abronsius
Professor Abronsius

Reputation: 33823

I would suggest generating the sql in a variable and testing that to see if it looks correct - even better to run the sql statement that is generated in your mysql client / gui / heidi etc:

$sql="INSERT INTO `teacher` (`schule`, `email`, `password`, `anrede`, `firstname`, `lastname`, `street`, `plz`, `city`, `phonenumber`)
VALUES ('$schul', '$emai', '$passwor' '$anred', '$vornam', '$nachnam', '$strass', '$pl', '$or', '$telefo')";

exit( $sql );/* Analyse this and test in heidi or similar */

$query1 = mysqli_query($connection, $sql );
if(!mysqli_query($connection, $query1) ) {
    exit( header("Location: /teachercp/index.php") );
}

Upvotes: 0

aldrin27
aldrin27

Reputation: 3407

Don't use die in your query: And pass the variable in your header:

   mysqli_query($connection,
    "INSERT INTO `teacher` (`schule`, `email`, `password`, `anrede`, `firstname`, `lastname`, `street`, `plz`, `city`, `phonenumber`)
     VALUES ('$schul', '$emai', '$passwor' '$anred', '$vornam', '$nachnam', '$strass', '$pl', '$or', '$telefo')"
   ); 

Upvotes: 0

Related Questions