I'm trying to build a social media (social network), and right now im trying to use tokens to do the login, but I keep having the error above. My code ran perfectly without the "token code". The error seems coming from inserting the token in database, but when I check the database the token was inserted correctly... Here's the code,and the full error message
(Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error in D:\xampp\htdocs\site\classes\DB.php:15 Stack trace: #0 D:\xampp\htdocs\site\classes\DB.php(15): PDOStatement->fetchAll() #1 D:\xampp\htdocs\site\classes\login.php(18): DB::query('INSERT INTO tok...', Array) #2 {main} thrown in D:\xampp\htdocs\site\classes\DB.php on line 15)
Login.php
<?php
//session_start();
include_once('DB.php');
if (isset($_POST['login']))
{
$email=$_POST['email'];
$password =$_POST['password'];
if (DB::query('SELECT email FROM users WHERE email=:email', array(':email' => $email)))
{
if (password_verify($password, DB::query('SELECT password FROM users WHERE email=:email', array(':email'=>$email))[0]['password']))
{
$cstrong = TRUE;
$token = bin2hex(openssl_random_pseudo_bytes(64,$cstrong));
$user_id = DB::query('SELECT id from users where email=:email', array(':email' => $email))[0]['id'];
DB::query('INSERT INTO tokens VALUES (\'\',:token,:user_id)',array(':token'=>sha1($token),':user_id'=>$user_id));
header('location: ');
setcookie("SNID",$token, time() + 60*60*24*7, '/',NULL,NULL,TRUE);
}
}
else
{
header('location: index.html');
}
}
DB.php
<?php
class DB
{
private static function connect()
{
$pdo = new PDO('mysql:host=127.0.0.1;dbname=pap;charset=utf8','root','');
$pdo -> setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
return $pdo;
}
public static function query($query,$params=array())
{
$statement = self::connect() ->prepare($query);
$statement ->execute($params);
$data = $statement -> fetchALL();
return $data;
}
}
?>
I hope you guys can help me out... Im still a learner so every help is welcome!