I am trying to select and insert into a database with PDO. When I pass my parameters in the url it works perfectly. But when there is nothing in the url, i get 2 errors.
Notice: Undefined index: username in C:\wamp64\www\MT\magiclogin.php on line 19
Notice: Undefined index: password in C:\wamp64\www\MT\magiclogin.php on line 20
Post works perfectly but i want to send it through the url and remove the html. But every time i reload the page i get the error. Here is my code
<?php
if($_SERVER['REQUEST_METHOD'] =="GET"){
try{
// new php data object
$handler = new PDO('mysql:host=127.0.0.1;dbname=magicsever', 'root', '');
//ATTR_ERRMODE set to exception
$handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e){
die("There was an error connecting to the database");
}
$username = trim($_GET['username']);
$password = trim($_GET['password']);
$stmt = $handler->prepare("SELECT * FROM generalusersdata WHERE username = ?");
$stmt->execute(array($username));
if($row = $stmt->fetch()){
$hashedPassword = md5(md5($row['user_id']).$_GET['password']);
if($hashedPassword == $row['password']){
$token = md5(uniqid(mt_rand(), true));
$stmtTokenCheck = $handler->prepare("SELECT * FROM token_table WHERE token = ?");
$stmtTokenCheck->execute(array($token));
if($rowToken = $stmtTokenCheck->fetch()){
$token = md5(uniqid(mt_rand(), true));
}
$time = time();
$stmt = $handler->prepare("INSERT INTO token_table (timestamp, user_id, token)VALUES(?, ?, ?)");
$stmt->execute(array($time, $row['user_id'], $token));
echo json_encode([
"timestamp" => $time,
"token" => $token,
"fullname" => $row['fullname'],
"username" => $row['username'],
"email" => $row['email']
]);
}else{
die("Password or Username entered is incorrect!");
}
}else{
die("Password or Username entered is incorrect!");
}
}
?>