修复寄存器PHP功能

时间:2018-06-19 06:34:46

标签: php html sql database function

我的register.php +我的注册功能正在运行,虽然没有收到错误而且没有自己发现错误。奇怪的是,它不会创建新用户,也无法登录。但是现在我只专注于让注册工作。

这里是register.php的代码

<?php

    include 'globals.php';
    include 'functions/registerfunc.php';
    $username = $_POST['username'];
    $passwort = $_POST['passwort'];
    $username = $_SESSION['username'];
    $passwort =  $_SESSION['passwort'];

    if (isset($_POST['register'])) {
        registerfunc();
    }
?>

<html>
<head>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
    <title> Coiffure Creation Registrierung </title>
</head>
<body>
    <a href="http://doriansandbox.toolmaster.ch">
        <img src="https://doriansandbox.toolmaster.ch/images/creation-coiffure-logo-neu.png"  align="middle">
    </a>
    <div id="top"></div>
    <div id="dashboard">
        <form method="POST">
            <ul>
                <li>
                    <label for="username">Username : </label>
                    <input type="text" id="username" maxlength="30" required autofocus name="username" />
                </li>
                <li>
                    <label for="passwort">Passwort : </label>
                    <input type="password" id="passwd" maxlength="30" required name="passwort" />
                </li>
                <li class="buttons">
                    <button type="submit" value="register">Registrieren</button>
                    <input type="button" name="submit" value="Abbrechen" onclick="location.href='index.php'" />
                </li>
            </ul>
        </form>
    </div>
</body 
</html>

这里是registerfunc函数

<?php
    function registerfunc(){
        include 'globals.php';
        $username = $_POST['username'];
        $passwort = $_POST['passwort'];
        $sql = "SELECT * FROM benutzer where username='".$username."'";
        $result1 = mysqli_query($con, $sql);

        if ((mysqli_num_rows($result1) <= 0)) {
            $passwort = password_hash($passwort, PASSWORD_DEFAULT);
            $query = "INSERT INTO benutzer(username, password) 
            VALUES('$username', '$passwort')";  
            if(mysqli_query($con, $query))
            {
                echo '<script>alert("Registrierung abgeschlossen")</script>';
                header("Location: ../index.php");

            }
        } else {

            echo "Fehler";
        }
    }
?>

2 个答案:

答案 0 :(得分:0)

也许是因为你的表单缺少了action属性。

<form method="POST" action="/register.php">

答案 1 :(得分:0)

name="register"添加到按钮

<button type="submit" name="register">Registrieren</button>

或者改变你的条件:

if (isset($_POST['username'])) {
    registerfunc();
}

或者这个:

if (isset($_POST['username']) && isset($_POST['passwort'])) {
    registerfunc();
}