Akar Akar - 2 months ago 8
PHP Question

$_POST[] without isset works fine but...

Hello I am new to php and mysql in general. but i have a problem Here is my problem:

I am trying to Insert data into database using forms.

but when i do, it returns 1 in username password columns.

Here is my Index.php:


$host = 'localhost';
$dbname= 'akar';
$user = 'akar';
$pass = 'raparen';

$dsn = "mysql:host=$host;dbname=$dbname";
$pdo = new PDO($dsn,$user,$pass);

$username = isset($_POST['username']);
$password = isset($_POST['password']);

$query = "INSERT INTO users (username,password) VALUES(:username,:password)";
$statement = $pdo->prepare($query);
':username'=> $username,
':password'=> $password


Here is my form:

<form action='index.php' method='post'>

<input type='text' placeholder='Enter Username here!' name='username' />
<input type='password' placeholder='Enter Password here!' name='password' />
<input type='submit' value='Submit' />

When i do this:

$username = $_POST['username'];

It Works fine but when i do this:

$username = isset($_POST['username']);

It inserts 1 in the database, and not the text in the input.

Sorry if i organize anything, i am newbie.


isset() returns a boolean value that is the result of the variable(s) being checked being set or not. It is not to be used to get values from a variable.

$username = isset($_POST['username']);
$password = isset($_POST['password']);

should be:

$username = $_POST['username'];
$password = $_POST['password'];

Or, if you want to assign a default value if those variables are not set, you would do it this way:

$username = isset($_POST['username']) ? $_POST['username'] : '';
$password = isset($_POST['password']) ? $_POST['password'] : '';