jacklee221 发表于 2012-2-17 21:22:25

老是报错,请高手看下。刚接触CI

本帖最后由 jacklee221 于 2012-2-17 21:25 编辑

============================控制器login.php========================
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller {
public function index()
{
$this->load->view('vlogin');
}

function checklogin(){
$this->load->model('checkuser');
$query = $this->checkuser->check();
if($query){
   echo 'ok';
   }else{
    echo 'false';
    }
}
}
==============================================================

============================模型checkuser.php======================
<?php
class Checkuser extends CI_Model{
function __construct()
   {
         parent::__construct();
   }


function check(){
   $username = $this->input->post('username',TRUE);
   $password = $this->input->post('password',TRUE);
   $query = $this->db->query("select * from user wher username=$username and userpass=$password");
   return $query->num_rows();
}
}
?>
==============================================================

============================视图vlogin.php========================
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body><form id="form1" name="form1" method="post" action="index.php/login/checklogin">
<table width="347" height="121" border="0" cellspacing="1">
<tr>
    <td>用户名</td>
    <td>
      <input type="text" name="username" id="username" />
   </td>
</tr>
<tr>
    <td>密码</td>
    <td><input type="text" name="password" id="password" /></td>
</tr>
<tr>
    <td> </td>
    <td><input type="submit" name="button" id="button" value="提交" /></td>
</tr>
</table> </form>
</body>
</html>
==============================================================

错误提示: Fatal error: Call to a member function num_rows() on a non-object in D:\wamp\www\admin\application\models\checkuser.php on line 13





同样的程序拷贝到以前的目录,又这样的报错


A Database Error OccurredError Number: 1054
Unknown column 'aaa' in 'where clause'
select * from user where username=aaa and userpass=bbb
Filename: D:\wamp\www\CodeIgniter\system\database\DB_driver.php
Line Number: 330



yuzhigang5460 发表于 2012-2-17 22:14:41

首先
$username = $this->input->post('username',TRUE);
   $password = $this->input->post('password',TRUE);
这两句应该在控制器里获得,$username和$password作为参数传递到check函数中。在你的代码里,sql的query语句where写的不对,另外加单引号。
推荐这么写
$query = $this->db->get_where('user', array('username'=>$username, 'password'=>$password));

jacklee221 发表于 2012-2-17 22:55:44

谢谢yuzhigang5460! 用get_where测试通过了。我想知道用$this->db->query( …………);这句究竟该如何写才能通过。

jacklee221 发表于 2012-2-18 15:51:50

谢谢yuzhigang5460! 用get_where测试通过了。我想知道用$this->db->query( …………);这句究竟该如何写才能通过。

hansonfox 发表于 2012-2-18 20:08:49

少了' 'username 和 userpass

lijun14992560 发表于 2012-2-21 18:03:48

$query = $this->db->query("select * from user wher username='$username' and userpass='$password'");
页: [1]
查看完整版本: 老是报错,请高手看下。刚接触CI