So I am trying to insert some data into a database with my own mysql class (built on top of pdo) but I keed getting some weird errors
( ! ) Fatal error: Uncaught PDOException: SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: 'Google Chrome' for column 'user_id' at row 1 in C:\wamp64\www\LVWeb\Core\Database\database.mysql-pdo.php on line 44
( ! ) PDOException: SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: 'Google Chrome' for column 'user_id' at row 1 in C:\wamp64\www\LVWeb\Core\Database\database.mysql-pdo.php on line 44
Table structure: http://prntscr.com/c3pgzi
My Query function:
public function Query($query, $vars = []){
$Statement = $this -> con -> prepare($query);
if(is_array($vars))
foreach($vars AS $key => $val){
if(is_string($val))
$Statement->bindParam($key, $val, PDO::PARAM_STR);
else if(is_integer($val))
$Statement->bindParam($key, $val, PDO::PARAM_INT);
}
return $Statement->execute();
}
The part where I try to insert the data:
$a = "INSERT INTO sessions (ses_id, user_id, ip, os, browser) VALUES
(:ses_id, :user_id, :ip, :os, :browser)";
$this->DB->Query($a, [
':ses_id' => $session,
':user_id' => $response['ID'],
':ip' => GetIp(),
':os' => GetOS(),
':browser' => GetBrowser()['name']
]);
So I have tested every variable out to see if they are the right type:
- $session = string
- $response['ID'] = integer
- GetIp() = string
- GetOs() = string
- GetBrowser()['name'] = string
Google Chromefor an integer. You made a classic mistake trying to make PDO "better" by creating some silly code that just makes it worse. Dump your function, use prepared statements directly. - N.B.