我有我的PDO/SQL这个奇怪的问题。我使用命名空间和类来控制事物。php pdo的成功,但不输入数据到数据库
我使用自定义PDO类,这是因为我也定义了一些标准值,如PDO应该抛出异常等等。
当我尝试创建用户时出现奇怪的问题。我正处于这个项目的开发阶段,因此我对测试登录进行了硬编码。
try
{
$user = new OOP\User\user();
$user->setFirstname("Firstname");
$user->setLastname("Lastname");
$user->setUsername("Test");
$user->setSecret(OOP\utillityClass::create_unique_hash(128));
$user->setPassword(OOP\utillityClass::create_password("*Duh as if im studpid enought to post my password*", $user->getSecret()));
$user->setEmail("some mail");
$birthday = new DateTime("1990-01-01");
$user->setBirthday($birthday);
$user->setClass(1);
$user->setAdded(new DateTime());
$user->setLast_Active(new DateTime());
$user->setActivated(true);
$user->setIp(OOP\utillityClass::getip());
$user->setIv(OOP\Crypt\crypt::createIv("hex"));
$permissions = array();
$permissions['staff'] = array();
$permissions['staff']['read'] = 1;
$user->setPermissions(json_encode($permissions));
$user->save();
}
catch (Exception $e)
{
echo "Error *censored, i use slurs while debugging*:" . PHP_EOL;
print_r($e);
}
以上是创建代码。
这是在数据库中的表:
CREATE TABLE IF NOT EXISTS `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`firstname` varchar(50) NOT NULL,
`lastname` varchar(50) NOT NULL,
`username` varchar(50) NOT NULL,
`password` varchar(128) NOT NULL,
`secret` varchar(128) NOT NULL,
`email` varchar(255) NOT NULL,
`birthday` date NOT NULL,
`class` int(11) NOT NULL,
`added` datetime NOT NULL,
`last_active` datetime NOT NULL,
`activated` tinyint(1) NOT NULL DEFAULT '0',
`ip` varchar(40) NOT NULL,
`iv` varchar(32) NOT NULL,
`permissions` text NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
这里来插入代码(注意,这是大众Save方法被调用,但保存方法调用这个私有方法,因为不存在用户加载的,那么它会创建它):
private function insert()
{
// We need the $CONFIG variable for sending actiovation emails \\
global $CONFIG;
$q = $this->PDO->prepare("INSERT INTO `user` (`firstname`, `lastname`, `username`, `password`, `secret`, `email`, `birthday`, `class`, `added`, `last_active`, `activated`, `ip`, `iv`, `permissions`) VALUES (:firstname, :lastname, :username, :password, :secret, :email, :birthday, :class, :added, :last_active, :activated, :ip, :iv, :permissions);");
$q->bindParam(':firstname', $this->firstname);
$q->bindParam(':lastname', $this->lastname);
$q->bindParam(':username', $this->username);
$q->bindParam(':password', $this->password);
$q->bindParam(':secret', $this->secret);
$q->bindParam(':email', $this->email);
$q->bindParam(':birthday', $this->birthday->format("Y-m-d"));
$q->bindParam(':class', $this->class);
$q->bindParam(':added', $this->added->format("Y-m-d H:i:s"));
$q->bindParam(':last_active', $this->last_active->format("Y-m-d H:i:s"));
$q->bindParam(':activated', $this->activated);
$q->bindParam(':ip', $this->ip);
$q->bindParam(':iv', $this->iv);
$q->bindParam(':permissions', $this->permissions);
$res = $q->execute();
if ($q->errorCode() != 00000)
{
throw new \Exception("An error occured during the creation");
}
else
{
return true;
}
}
现在,这里是开心的事,没有什么异常或PDOException的根本,实际上它通过,因为它应该,但它不会将数据输入到数据库中,但自动增加数量增加1.
我真的很希望有人能帮助我解决这个问题,因为我一直试图让这个工作整天,我试图删除并重新创建数据库和表,包括插入代码。
在此之前,非常感谢您的时间和帮助。
愚蠢的问题,但所有的类变量设置好吗? – andrewsi
这个'$ user = new OOP \ User \ user();'被误解了(我可能错了)。 'User'和'user',但那只是我。看看'andrewsi'提到了什么。 –
尝试使用$ this-> PDO-> setAttribute(PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION)设置调用的错误模式;然后,将整个查询执行包装在try catch块中,而不是查看错误代码。此外,如果它在事务中,您将需要提交更改。 – x20mar