0

Hello.
Look at this file please:
register.php

<?php
$servername = "localhost";
$dbname = "mydbname";
$dbusername = "mydbusername";
$dbpassword = "mydbpassword";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $dbusername, $dbpassword);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql = "INSERT INTO Users (Username, Password)
    VALUES ('$username', '$password')";

    $conn->exec($sql);
    }
catch(PDOException $e)
    {
    echo $sql . "<br>" . $e->getMessage();
    }

$conn = null;
?>

<html>
<head>
    <title>Register Page</title>
</head>
<body>
<form method="post">
Your Username: <input type="text" name="username"><br>
Your Password: <input type="text" name="password"><br>
<input type="submit" value="submit">
</form>
</body>

Why it can't send the username and password that gets from those two fields to the table? I checked the table, the ID was incresed everytime but the fileds of username and password were empty on the table.

2
Contributors
4
Replies
17
Views
2 Years
Discussion Span
Last Post by lps
0

I think the reason must be the action= wich is not in the <form> tag. The action sets the Destination where data should be sent to, correct? So here that the Destination is not an other php file, is db table, what can i type for action for the <form> tag?

2

The action is not a mandatory as if it is not set, the form will just post to same file. The problem here mostly is about the part

VALUES ('$username', '$password')";

where these 2 variables is not set to anything yet. Perhaps adding these lines before the query will help.

$username = $_POST['username'];
$password = $_POST['password'];
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.