L.e.e. 发表于 2013-5-22 09:03:04

无法提交数据到数据库

本帖最后由 L.e.e. 于 2013-5-22 12:51 编辑


提交的话,会出现下面的错误,到底哪里错了啊?我在 database.php 里 设置过了 基本的 数据库参数。刚开始研究这个框架3天 有很多 不懂的地方 以后请多多指教~ 附件里有 提交之前的 表单图片

A Database Error OccurredUnable to connect to your database server using the provided settings.Filename: D:\wwwroot\ftp87315\Web\system\database\DB_driver.phpLine Number: 124
我现在的代码。如下:
application/controllers/welcome.php 页面
class Welcome extends CI_Controller {
        function index(){
          $this->load->helper('form');
          $data['title'] = "Welcome to our Site";
          $data['headline'] = "Welcome!";
          $data['include'] = 'home';
          $this->load->vars($data);
          $this->load->view('template');
        }

        function contactus(){
                $this->load->helper('url');
                if ($this->input->post('email')){
                        $this->load->model('MContacts','',TRUE);
                        $this->MContacts->addContact();
                        redirect('welcome/thankyou','refresh');
                }else{
                        redirect('welcome/index','refresh');
                }
        }
        function thankyou(){
                $data['title'] = "Thank You!";
                $data['headline'] = "Thanks!";
                $data['include'] = 'thanks';
                $this->load->vars($data);
                $this->load->view('template');
        }

}

application/models/mcontacts.php 页面
class MContacts extends CI_Model{

function __construct(){
    parent::CI_Model();
}

function addContact(){
$now = date("Y-m-d H:i:s");
$data = array(
    'name' => $this->input->xss_clean($this->input->post('name')),
    'email' => $this->input->xss_clean($this->input->post('email')),
    'notes' => $this->input->xss_clean($this->input->post('notes')),
    'ipaddress' => $this->input->ip_address(),
    'stamp' => $now
);

$this->db->insert('contacts', $data);
}
}
application/views/template.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" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title><?php echo $title;?></title>
<style>label { display:block;}</style>
</head>
<body>
<h1><?php echo $headline;?></h1>
<?php $this->load->view($include);?>

application/views/home.php 页面
<p>This is random text for the CodeIgniter article.
There's nothing to see here folks, just move along!</p>
<h2>Contact Us</h2>
<?php
echo form_open('welcome/contactus');
echo form_label('your name','name');
$ndata = array('name' => 'name', 'id' => 'id', 'size' => '25');
echo form_input($ndata);

echo form_label('your email','email');
$edata = array('name' => 'email', 'id' => 'email', 'size' => '25');
echo form_input($edata);

echo form_label('how can you help you?','notes');
$cdata = array('name' => 'notes', 'id' => 'notes', 'cols' => '40', 'rows' => '5');
echo form_textarea($cdata);

echo form_submit('submit','send us a note');
echo form_close();
?>
</body>
</html>

startbbs 发表于 2013-5-22 09:29:43

数据库没有联接成功,再检查一下配置,如果配置没有问题的话,再看看是否加载数据库了

$this->load->database();

patrick155 发表于 2013-5-22 11:46:19

上代码, 你没有代码 就只能猜测 你哪里有问题,楼上的已经说了大概的可能了。

根据英文提示 你的数据库 配置有问题

L.e.e. 发表于 2013-5-22 12:35:50

startbbs 发表于 2013-5-22 09:29 static/image/common/back.gif
数据库没有联接成功,再检查一下配置,如果配置没有问题的话,再看看是否加载数据库了

$this->load->datab ...

$this->load->database(); 数据库参数是对 但是无法加载是为什么?

L.e.e. 发表于 2013-5-22 12:41:34

patrick155 发表于 2013-5-22 11:46 static/image/common/back.gif
上代码, 你没有代码 就只能猜测 你哪里有问题,楼上的已经说了大概的可能了。

根据英文提示 你的数据库 ...

哦~能不能 远程控制 帮忙看一下啊?

patrick155 发表于 2013-5-22 14:19:21

L.e.e. 发表于 2013-5-22 12:41 static/image/common/back.gif
哦~能不能 远程控制 帮忙看一下啊?

在公司 不方便远程连接。

你只需要发出你数据库配置代码以及使用数据库连接 以及处理的代码即可

CI 的数据工具类 基本使用上 没有什么问题的 只要你符合规则就行了

patrick155 发表于 2013-5-22 14:27:13

<?php
class Blog extends CI_Controller {

function __construct()
{
parent::__construct();
}

public function index()
{
echo 'Hello World!';
}
}
?>

这个是 controller 官方事例你是否发觉自己少了一点东西

function __construct()

构造函数

L.e.e. 发表于 2013-5-22 14:30:02

patrick155 发表于 2013-5-22 14:27 static/image/common/back.gif
这个是 controller 官方事例你是否发觉自己少了一点东西

function __construct()


parent::__construct(); 加上这一句 也不能 连接到数据库啊

startbbs 发表于 2013-5-22 14:42:26

把$this->load->database(); 加到parent::__construct(); 下面试一下

patrick155 发表于 2013-5-22 14:49:06

<?php
class MContacts extends CI_Model{

        function __construct()
    {
      parent::__construct();
      $this->load->helper('security');
    }

function addContact(){
$now = date("Y-m-d H:i:s");
$data = array(
    //'name' => $this->input->xss_clean($this->input->post('name')),
    //'email' => $this->input->xss_clean($this->input->post('email')),
    //'notes' => $this->input->xss_clean($this->input->post('notes')),
    'name' => $this->security->xss_clean($this->input->post('name')),
    'email' => $this->security->xss_clean($this->input->post('email')),
    'notes' => $this->security->xss_clean($this->input->post('notes')),
    'ipaddress' => $this->input->ip_address(),
    'stamp' => $now
);

$this->db->insert('contacts', $data);
}
}
页: [1] 2 3
查看完整版本: 无法提交数据到数据库