• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A Database Error Occurred[newbie user]

#1
I creating my school project. I'm reimplement my project using codeigniter.
I've encountered errors. This error was the result after executing this $this->db->get(); in model.


A Database Error Occurred

Error Number:

SELECT "username", "pass" FROM "users", "users" WHERE UPPER(username) = 'POWERUSER' AND "pass" = '63a9f0ea7bb98050796b649e85481845'

Filename: C:\Program Files (x86)\wamp\www\Database\system\database\DB_driver.php

Line Number: 330


---------------------------------------------------------------------------------------------------------------------------
Model: Model_users

<?php
class Model_users extends CI_Model{
public function can_log_in($inputData){
echo '<script type = "text/javascript">alert("The result is: '.$inputData['username'].'");</script>';
$this->db->select('username, pass');
$this->db->from('users');
echo '<script type = "text/javascript">alert("The result 2 is: '.$inputData['username'].'");</script>';
$this->db->where('UPPER(username)',strtoupper($inputData['username']));
$this->db->where('pass', (md5('root')));
//$this->db->get('users');
echo '<script type = "text/javascript">alert("Waiting to be processed");</script>';
if($this->db->get('users')->num_rows() > 0){
echo '<script type = "text/javascript">alert("Query successfully executed");</script>';
return $this->db->get('users')->result();
}else{
return false;
}
}
}
?>


-------------------------------------------------------------------------------------------------------------------------

Controller: Login.php


<?php
class Login extends CI_Controller {

   public function view($page = 'login')
   {
//Load the window for Login
$this->load->helper('url');
$this->load->view($page);
   }
public function transaction(){
$this->load->view('process/header');
$this->load->view('process/body');

}
public function logout(){
$sess_array = array(
'username' => ''
);

$this->session->unset_userdata('logged_in',$sess_array);
// $data['message_display'] = 'Successfully Logout';
redirect('../database/index.php','refresh');
//$this->load->view('login',$data);
}
//validate details
public function login_validation(){
$this->load->model('model_users');
echo '<script type = "text/javascript">alert("Loaded database");</script>';
$inputData['username'] = $this->input->post('username');
$inputData['password'] = $this->input->post('password');
echo '<script type = "text/javascript">alert("Passed value");</script>';
$result = $this->model_users->can_log_in($inputData);
echo '<script type = "text/javascript">alert("The result is: '.$result.'");</script>';

if($result){
foreach($result as $row){
$sessionData = array('username' => $row->username);
}
$this->session->set_userdata($sessionData);
$this->load->view('main\index\header');
//$this->load->view('main\index\body');
// $this->load->view('default\footer');
}else{
redirect('index.php/login/transaction','refresh');
}
$this->load->view('default\footer');
}
public function validate_credentials(){
$this->load->model('model_users');
if($this->model_users->can_log_in()){

return true;

}else{
$this->form_validation->set_message('validate credentials','Incorrect username/password.');
return false;
}
}
}


----------------------------------------------------------------------------------------------------------------------

View: login.php



<html>
<head>

<title>
Login
</title>

<?php
$page = $_SERVER['PHP_SELF'];
$sec = "72000";
?>
<meta http-equiv="refresh" content="<?php echo $sec?>;URL='<?php echo $page?>'">
<link href="<?php echo base_url("source_assets/css/bootstrap.css"); ?>" rel="stylesheet">
<link href="<?php echo base_url("source_assets/css/bootstrap.min.css"); ?>" rel="stylesheet">
<link href="<?php echo base_url("source_assets/css/prettyPhoto.css"); ?>" rel="stylesheet">
<link href="<?php echo base_url("source_assets/css/font-awesome.css"); ?>" rel="stylesheet">
<link href="<?php echo base_url("source_assets/css/main.css"); ?>" rel="stylesheet">
<link href="<?php echo base_url("source_assets/css/social.css"); ?>" rel="stylesheet">

<script src = "source_assets/js/jquery-1.11.1.js"></script>
<script src = "source_assets/js/bootstrap.js"></script>
<script src = "source_assets/js/ie10-viewport-bug-workaround.js"></script>
<script src = "source_assets/js/ie-emulation-modes-warning.js"></script>
<script src = "source_assets/js/bootstrap.min.js"></script>


</head>

<script src="settings/js/ie-emulation-modes-warning.js"></script>


<body background = "../database/source_assets/img/bg-head.jpg">
<br/> <br/> <br/>
<center>
<a href = "../../../index.php">
<img height = "10%"src = "../../../source_assets/img/220 copy.png" title = "Click me to return home"/>
</a>
</center>

<div style = "margin-left:32%"class = "col-md-12">
<?php //echo form_open('index.php/login/login_validation');

//echo form_close();?>
<form action = "../../../index.php/login/login_validation" method = "post">
<div class = "col-md-4">
<div class = "form-group has-error">
<input type = "text" id = "username" name = "username" class = "form-control" placeholder = "username" value = "" required>
</div>
<div class = "form-group has-error">
<input type = "password" name = "password" class = "form-control" placeholder = "password" value = "" required | md5>
</div>
<button type = "submit" class = "btn btn-danger btn-block">Login</button>

</div>
</form>
</div>
</div><!-- /container -->
</body>
</html>


----------------------------------------------------------------------------------------------------------------------

Table: users
----------------------------------------------
|    userid     |    username     |    password     |
----------------------------------------------
|       1        |   poweruser     |      root          |
----------------------------------------------



I'm hoping for your respones. To all codeigniter users


Attached Files
.php   Login.php (Size: 1.77 KB / Downloads: 117)
.php   Model_users.php (Size: 849 bytes / Downloads: 116)
.php   config.php (Size: 12.5 KB / Downloads: 116)
Reply

#2
PHP Code:
class Model_users extends CI_Model
{
 
   public function can_log_in($inputData)
 
   {
 
       echo '<script type = "text/javascript">alert("The result is: '.$inputData['username'].'");</script>';

 
       $this->db->select('username, pass');
 
       $this->db->from('users');

 
       echo '<script type = "text/javascript">alert("The result 2 is: '.$inputData['username'].'");</script>';

 
       $this->db->where('UPPER(username)',strtoupper($inputData['username']));
 
       $this->db->where('pass', (md5('root')));
 
       
        
//$this->db->get('users');
 
       
        echo 
'<script type = "text/javascript">alert("Waiting to be processed");</script>';
 
       
        if 
($this->db->get('users')->num_rows() > 0) {
 
           echo '<script type = "text/javascript">alert("Query successfully executed");</script>';
 
           return $this->db->get('users')->result();
 
       } else {
 
           return false;
 
       }
 
   }


I'm assuming the echos here are just because you're troubleshooting, but, even in that case, they shouldn't be in the model. Even without them, though, this is where the problem occurs. The table name for your query is defined when calling either the db->from() method or the db->get() method, you have it defined in both, so it's being added to your query twice.

Further, you're probably not going to get the result you're looking for when your query succeeds, because you're calling $this->db->get('users') again after you check the number of rows for your result (and this time, it will be "select * from users" without a where clause).

To fix this, you would probably want to do something like this:
PHP Code:
class Model_users extends CI_Model
{
 
   public function can_log_in($inputData)
 
   {
 
       $this->db->select('username, pass');
 
       $this->db->from('users');
 
       $this->db->where('UPPER(username)'strtoupper($inputData['username']));
 
       $this->db->where('pass', (md5('root')));
 
       
        $query 
$this->db->get();
 
       
        if 
($query->num_rows() > 0) {
 
           return $query->result();
 
       } else {
 
           return false;
 
       }
 
   }


Beyond this, the idea that this code might have something to do with handling usernames and passwords for your site is scary. For password hashing, start with this: http://php.net/manual/en/faq.passwords.php

If you can't use one of the versions of PHP supported by that information for some reason, this should help: http://www.openwall.com/phpass/
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.