2012-09-04 75 views
2

我正在尝试使用codeigniter将数据添加到数据库。我已经使用主动方法设置了一个注册页面,并尝试使用相同的方法来添加新闻表单,但未成功。使用codeigniter将数据添加到数据库中

当我点击提交它说的网页无法找到,网址显示控制器的功能名称。当我故意留下任何字段为空时,这也是一样的。我检查了我的数据库,没有添加记录,也没有记录php错误。

这里是我的代码片段:

查看:

<?php echo form_open('add/add_article'); ?> 
     <?php echo form_input('title', set_value('title', 'Title')); ?><br /> 
     <?php echo form_textarea('content', set_value('content', 'Content')); ?><br /> 
     <?php echo form_input('author', set_value('author', 'Author')); ?> 
     <?php echo form_submit('submit', 'Add Article'); ?> 
     <?php echo validation_errors('<p class="error">');?> 
    <?php echo form_close(); ?> 

控制器:

class Add extends CI_Controller { 

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

    public function index() { 
     $this->load->view('admin/add'); 
} 

    public function add_article() { 
     $this->load->library('form_validation'); 

     $this->form_validation->set_rules('title', 'Title', 'trim|required'); 
     $this->form_validation->set_rules('content', 'Content', 'trim|required'); 
     $this->form_validation->set_rules('author', 'Author', 'trim|required'); 

     if($this->form_validation->run() == FALSE) { 

     $this->index(); 

     }else{ 

      $this->load->model('news_model'); 
      if($query = $this->news_model->addArticle()) { 
      $this->load->view('news'); 
      }else { 
       $this->load->view('news'); 
     } 
    } 
} 
} 

型号:

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

     function addArticle() { 
    $data =array(
    'title' => $this->input->post('title'), 
    'content' => $this->input->post('content'), 
    'author' => $this->input->post('author'), 
    'username' => $this->input->post('username')); 

    $insert = $this->db->insert('news', $data); 
    return $insert; 
} 
} 
+0

你在表单的源代码中看到了什么? –

+0

在源文件中,我知道该动作是针对add控制器和add_article函数的,当它提交时它只显示:localhost/codeigniter/add/add_controller,然后找不到页面,是什么意思?我也尝试在if语句中的控制器中使用重定向。 – Shimsham84

+0

试试'echo form_open('index.php/add/add_article');' –

回答

3

如果是服务器抛出未找到该页面,则几乎肯定是URL问题,而不是CI/PHP问题。

您的基础URL是否在配置文件中正确定义?您的.htaccess配置是否正确(旧的配置可能会将请求从CI路由/添加)?

尝试添加以下行动来添加控制器,并直接在http://[base]/add/thetest

public function thetest() { 
echo 'Controller accessed'; 
die; 
} 

导航到它,如果它仍然说找不到网页这不是你的代码,这是你的配置(无论是服务器配置或CI)。

+0

我试过这个,当测试它仍然扔了一个CI页面URL没有找到我添加管理添加/测试和回声被发现,所以现在我已经添加了“管理员”的窗体打开,如果查询在我的控制器。谢谢。 – Shimsham84

0

而是插入使用更新的模型如:

$insert = $this->db->update('news', $data); 
return $insert; 

而且我认为,在控制你的这部分代码是错误太(错误的if语句,并没有数据发送到模型):

if($query = $this->news_model->addArticle()) { 
      $this->load->view('news'); 
      }else { 
       $this->load->view('news'); 
     } 

试试这个:

$data =array(
    'title' => $this->input->post('title'), 
    'content' => $this->input->post('content'), 
    'author' => $this->input->post('author'), 
    'username' => $this->input->post('username') 
); 

$query = $this->news_model->addArticle($data); 
if($query) 
{ 
    // query ok 
    $this->load->view('news'); 
} 
else { 
    // no query 
    $this->load->view('news'); 
} 
+0

我已经保存了这个片段以备将来引用,但是当使用$ insert = $ this-> db-> update('news',$ data);这更新了数据库中的所有记录,而不是添加新行。 – Shimsham84

相关问题