0

I am trying to create a user registration form with PHP, though it is not working. I am getting no error messages and no indication of what is wrong. Can you please help me?

<!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>
Qirel
  • 25,449
  • 7
  • 45
  • 62
Safal R. Aryal
  • 739
  • 1
  • 6
  • 11
  • a) post code here, b) how do you get "Undefined index:" and have no error messages? –  Dec 12 '16 at 03:40
  • You should also use prepared statements for this - as this snippet of code is now, it's vulnerable to SQL injection. Kickoff: http://php.net/manual/en/mysqli.prepare.php – Qirel Dec 12 '16 at 03:42
  • Doesn't look like the "*Undefined index...*" is coming from the form anyway, you sure it's the right code you're showing us? What exact index is undefined? – Qirel Dec 12 '16 at 03:46

1 Answers1

0

You're passing through 4 values, but only assigning 3 fields.

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

Should be something like:

"INSERT INTO users (username, password, email, ip_address)
VALUES ('$uname', '$passwd', '$email', '".$_SERVER['REMOTE_ADDR']."')"
Daerik
  • 4,167
  • 2
  • 20
  • 33