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.

Recommended Answers

All 4 Replies

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?

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'];

Yesssss! It works :)
Thank you @lps.

If the issue is solve, please do mark this article as 'solved'. Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.