所以我正在创建一个使用php类将数据插入数据库的登录系统。以下是数据库的插入方法。
public function insert($table, $fields = array()){
if(count($fields)){
$keys = array_keys($fields);
$values = '';
$x = 1;
foreach($fields as $field){
$values .= '?';
if($x < count($fields)){
$values .= ', ';
}
$x++;
}
$sql = "INSERT INTO {$table} (".implode(', ', $keys).") VALUES ({$values})";
if(!$this->query($sql, $fields)->error()){
return true;
}
}
return false;
}
下一个方法是我抓住输入字段输入数据库。
public function create($fields = array()) {
if ($this->_db->insert('users', $fields)) {
throw new Exception('There was a problem with creating an account. Try again later.');
}
}
这就是我注册用户的方式。
if ($validation->passed()) {
//register user
$user = new User();
try {
$salt = Hash::salt(32);
$address =
Input::get('street_address') . ':' .
Input::get('city') . ':' .
Input::get('state_province') . ':' .
Input::get('zip_code')
;
echo $address;
$user->create(array(
'first_name' => Input::get('first_name'),
'last_name' => Input::get('last_name'),
'email' => Input::get('email'),
'address' => $address,
'password' => Hash::make(Input::get('password'), $salt),
'salt' => $salt,
'date_joined' => date('Y-m-d H:i:s'),
'groups' => 1
));
Session::flash('home', 'You have been registered and can now log in!');
header('Location: index.php');
} catch(Exception $e) {
die($e->getMessage());
}
// Session::flash('success', 'You registered successfully!');
// header('Location: index.php');
} else {
//show errors
foreach($validation->errors() as $error) {
echo $error . "<br>";
}
}
我尝试修复查询并确保没有错误,但我似乎无法找到任何错误。