未定义的索引:PHP表单错误?

时间:2016-12-12 03:37:06

标签: php mysql

我正在尝试使用PHP创建用户注册表单,尽管它不起作用。我没有收到任何错误消息,也没有任何错误的迹象。你能帮我吗?

<!doctype html>
<html>
<head>
</head>
<body>
<form method="POST" enctype="multipart/form-data" action="<?PHP echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" accept-charset="UTF-8">
    <h4><label>Sign up</label></h4>
    <p><label>Username<strong>*</strong><br>
            <input type="text" size="48" name="username"></label></p>
    <p><label>Password<strong>*</strong><br>
            <input type="password" size="48" name="password" ></label></p>
    <p><label>Email<br>
            <input type="email" size="48" name="email"></label></p>
    <p><input type="submit" name="sendfeedback" value="Sign up"></p>
</form>
<?php
$servername = "localhost";
$username = "safsom2";
$password = "somil888";
$dbname = "appDB";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
    die("Failed connecting to user database : " . $conn->connect_error);
}
echo 'Success connecting to user database.';
if (isset($_POST['sendfeedback'])) {
    $uname = $_POST['username'];
    $passwd = $_POST['password'];
    $email = $_POST['email'];
    if (!$conn->query("INSERT INTO users (username, password, email)
VALUES ('$uname', '$passwd', '$email', '".$_SERVER['REMOTE_ADDR']."')") ) {
        echo '<br>Failed account creation';
    }
    else {
        echo '<br>Created account';
    }
}
?>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

您正在传递4个值,但只分配3个字段。

"INSERT INTO users (username, password, email)
VALUES ('$uname', '$passwd', '$email', '".$_SERVER['REMOTE_ADDR']."')"

应该是这样的:

"INSERT INTO users (username, password, email, ip_address)
VALUES ('$uname', '$passwd', '$email', '".$_SERVER['REMOTE_ADDR']."')"