用户
 找回密码
 入住 CI 中国社区
搜索
查看: 4948|回复: 23
收起左侧

[HELP] 无法提交数据到数据库

[复制链接]
发表于 2013-5-22 09:03:04 | 显示全部楼层 |阅读模式
本帖最后由 L.e.e. 于 2013-5-22 12:51 编辑


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

A Database Error Occurred

Unable to connect to your database server using the provided settings.

Filename: D:\wwwroot\ftp87315\Web\system\database\DB_driver.php

Line 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>
fdsafsdf.PNG
发表于 2013-5-22 09:29:43 | 显示全部楼层
数据库没有联接成功,再检查一下配置,如果配置没有问题的话,再看看是否加载数据库了

$this->load->database();
发表于 2013-5-22 11:46:19 | 显示全部楼层
上代码, 你没有代码 就只能猜测 你哪里有问题,楼上的已经说了大概的可能了。

根据英文提示 你的  数据库 配置有问题
 楼主| 发表于 2013-5-22 12:35:50 | 显示全部楼层
startbbs 发表于 2013-5-22 09:29
数据库没有联接成功,再检查一下配置,如果配置没有问题的话,再看看是否加载数据库了

$this->load->datab ...

$this->load->database(); 数据库参数是对 但是  无法加载是为什么?
 楼主| 发表于 2013-5-22 12:41:34 | 显示全部楼层
patrick155 发表于 2013-5-22 11:46
上代码, 你没有代码 就只能猜测 你哪里有问题,楼上的已经说了大概的可能了。

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

哦~能不能 远程控制 帮忙看一下啊?
发表于 2013-5-22 14:19:21 | 显示全部楼层
L.e.e. 发表于 2013-5-22 12:41
哦~能不能 远程控制 帮忙看一下啊?

在公司 不方便远程连接。

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

CI 的数据工具类 基本使用上 没有什么问题的 只要你符合规则就行了
发表于 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()

构造函数
 楼主| 发表于 2013-5-22 14:30:02 | 显示全部楼层
patrick155 发表于 2013-5-22 14:27
这个是 controller 官方事例  你是否发觉自己少了一点东西

function __construct()

parent::__construct(); 加上这一句 也不能 连接到数据库啊
发表于 2013-5-22 14:42:26 | 显示全部楼层
把$this->load->database(); 加到parent::__construct(); 下面试一下
发表于 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);
}
}

本版积分规则