【问题标题】:values not getting stored in database using codeigniter未使用 codeigniter 将值存储在数据库中
【发布时间】:2018-06-27 14:14:13
【问题描述】:

我正在使用 codeigniter 概念将记录保存在数据库中,我对此编码有一点问题,请任何人帮助我,因为我是这个概念的新手, 表单验证工作正常,但是当我尝试插入和提交时,数据没有存储在数据库中,我没有遇到我面临的问题 谁能帮我解决这个问题。

Home.php(控制器文件)

    <?php  
    defined('BASEPATH') OR exit('No direct script access allowed');  

    class Home extends CI_Controller {
        public function __construct() 
        {
            parent::__construct();

            //load database libray manually
            $this->load->database();

            //load Model
            $this->load->model('Contact_model');

            // load form and url helpers
            $this->load->helper(array('form', 'url'));

            // load form_validation library
            $this->load->library('form_validation');
        }

        public function contact()  
        {  
            // Basic validation
            $this->form_validation->set_rules('name', 'Name', 'required');
            $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
            $this->form_validation->set_rules('subject', 'Subject', 'required');
            $this->form_validation->set_rules('message', 'Message', 'required');

            if ($this->form_validation->run() == FALSE)
            {
                $this->load->helper('url');
                $this->load->view('header');
                $this->load->view('contact'); 
                $this->load->view('footer');
            }
            else
            {
                /* load success template...
                echo "Thanks For Contacting!";*/
                //insert
                if($this->input->post('submit'))
                {
                    $name=$this->input->post('name');
                    $email=$this->input->post('email');
                    $subject=$this->input->post('subject');
                    $message=$this->input->post('message');
                    $this->Contact_model->saverecords($name,$email,$subject,$message);  
                    echo "Records Saved Successfully";
                }
            }
        }

    }  
    ?>

contact_model.php(模型文件)

    <?php
        class Contact_model extends CI_Model 
        {
            //Insert
            function saverecords($name,$email,$subject,$message)
            {
                $query="insert into contact values('','$name','$email','$subject','$message')";
                $this->db->query($query);
            }
        }
    ?>

【问题讨论】:

  • 你试过检查你的错误日志吗?
  • 不知道如何检查它
  • 检查您的 application/logs/ 目录或 Apache/Nginx 错误日志。
  • 在查询中删除你传递的空值 $query="insert into contact values('$name','$email','$subject','$message')";
  • 我不明白先生

标签: php view model controller


【解决方案1】:

您的查询似乎是错误的,您需要描述您正在更新的列我想格式应该是

$sql = "INSERT INTO foobar(firstname, lastname, email) VALUES ('John', 'Doe', 'john@example.com')";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-18
    • 2013-05-06
    • 2019-08-03
    • 2011-02-16
    相关资源
    最近更新 更多