PHP表单到数据库 - 不工作

时间:2015-08-22 08:40:41

标签: php html mysql twitter-bootstrap

我再一次有一个表单要我提交到我的数据库,但它不起作用,我真的不知道为什么。我测试了所有内容,但我在代码中看不到任何错误。

以下是我的表格代码:

<!-- 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 -->

这是我的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连接正常。我也没有收到任何错误消息。每当我点击&#34;注册&#34;时,我都会转到register_teacher.php上的空白页面!重定向到teachercp / index.php不起作用,我的数据库中没有数据。

有什么建议吗?

谢谢, 克里斯

3 个答案:

答案 0 :(得分:1)

我在本地服务器上测试了你的代码。它显示了这个错误:

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

我的代码中出现的错误是:

你错过了第39行的逗号。查看我错过它的图片。 enter image description here

答案 1 :(得分:0)

不要在查询中使用die:并在标题中传递变量:

   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')"
   ); 

答案 2 :(得分:0)

我建议在变量中生成sql并测试它以查看它是否正确 - 甚至更好地运行在mysql客户端/ gui / heidi等中生成的sql语句:

$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") );
}