我遇到了SQLSTATE [HY000]的问题:一般错误,我的代码工作得很好,它更新插入和选择但仍然得到错误。
我收到的最后一个错误如下:
Marker - 11 Jul 2016 16:34:44
[11-Jul-2016 14:58:24 Europe/Berlin] PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error' in /Users/mughery/Documents/Business/websites/LoginBusinessDoor /_/class.Database.inc:46
Stack trace:
#0 /Users/mughery/Documents/Business/websites/LoginBusinessDoor/_/class.Database.inc(46): PDOStatement->fetchAll(5)
#1 /Users/mughery/Documents/Business/websites/LoginBusinessDoor/_/class.Database.inc(147): Database->query('UPDATE customer...', Array)
#2 /Users/mughery/Documents/Business/websites/LoginBusinessDoor/index.php(116): Database->update('customer', '_customer_id', 10, Array)
#3 {main}
thrown in /Users/mughery/Documents/Business/websites/LoginBusinessDoor/_/class.Database.inc on line 46
我一直在关注教程,本教程中的代码运行正常,但我的代码没有。可能是什么问题?
我的代码如下。我收到错误,Redirect也无效。
public function query($sql, $params = array()){
$this->_error = false;
if($this->_query = $this->_connection->prepare($sql)){
$x = 1;
if(count($params)){
foreach($params as $param ){
$this->_query->bindValue($x, $param);
$x++;
}
}
if($this->_query->execute()){
$this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
$this->_count = $this->_query->rowCount();
}
else{
$this->_error = true;
}
}
return $this;
}
插入方法如下:
public function insert($table, $fields=array()){
$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;
}
这是调用代码的上下文:
<?php
ob_start();
require_once "core/init.php";
$output ='';
//var_dump(Token::check(Input::get('token')));
if(Input::exists()){
//if(Token::check(Input::get('token'))){
$validate = new Validation();
$validatation = $validate->check($_POST,
array(
'username' =>array(
'requierd'=>true,
'min'=>5,
'max'=>400,
'unique'=>'customer'),
'uname' =>array(
'requierd'=>true,
'min' => 5,
'max' => 50
),
'pwd' =>array(
'requierd'=>true,
'min'=> 9
),
'confirm'=>array(
'requierd'=> true,
'matches' => 'pwd'
),
)
);
//Storing error bellow.
if($validate->passed()){
//$output.= 'You have been successfully registerd';
$customer = new Customer();
$salt = Hash::salt(32);
try{
$customer->create(array(
'username' =>Input::get('username'),
'password' =>Hash::make(Input::get('pwd'), $salt),
'salt' =>$salt,
'name' =>Input::get('uname'),
'joined' =>date('Y-m-d H:i:s'),
'group' =>1
));
Session::flash('home', 'You have been registerd and can now log in!');
Redirect::to('index.php');
}catch(Exception $e){ die($e->getMessage()." Here is the error "); }
}else{
$output.='<div class="col-md-12"><div class="jumbotron"><p>';
//output errors
$x =0;
foreach($validate->errors() as $key => $error){
$output.= $error. ', ';
}
$output.='</p></div></div>';
}
//}
}
?>
<?php include"_/php/header.php";?>
<?php echo $output; ?>
<div class="col-md-3"></div>
<div class="col-md-6">
<div class="panel panel-default">
<div class="panel-heading">
<h2> Sign Up</h2>
</div>
<form class="panel-body" action="signup.php" method="post">
<div class="form-group">
<label for="email">Email</label>
<input class="form-control" type="email" name="username" id="username" placeholder="Email" value="<?php echo escap(Input::get('username')); ?>"/>
</div>
<div class="form-group">
<label for="email">Username</label>
<input class="form-control" type="uname" name="uname" id="uname" placeholder="Username" value="<?php echo escap(Input::get('uname')); ?>"/>
<span>This Name will appear when your application is listed, your real name wont be seen except by the bankers. </span>
</div>
<div class="form-group">
<label for="password">Create Password</label>
<input class="form-control" type="text" name="pwd" id="password" placeholder="Password"/>
</div>
<div class="form-group">
<label for="repassword">Confirm Password</label>
<input class="form-control" type="text" id="repassword" name="confirm" placeholder="Password" value=""/>
</div>
<div class="checkbox">
<label>
<input id="terms" name="terms" value="off"type="checkbox" unchecked>
Terms and Condition
</label>
</div>
<input type="hidden" name="token" value="<?php echo Token::generate();?>">
<input class="btn btn-default btn-lg" type="submit" name="register" value="Sign Up">
</form>
<div class="panel-footer">
<h4>Have an account? Log in<h4>
</div>
</div>
</div><!--End col-md-6 -->
<div class="col-md-3"></div>
</div><!--End row-->
</section><!--End section-->
<!-- Javascript -->
<script src="js/jQuery.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
最后两个代码位于同一个文件signup.php。