php - 无法使用codeigniter在Mysql中插入数据

标签 php mysql database codeigniter

由于数据库错误 1048,我无法将数据插入 mysql。我已经搜索了将近 1 周,但找不到任何解决方案。请帮我。这是所有的东西......

Controller users.php:

<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Users extends CI_Controller 
{
    function __construct()
    {
        parent::__construct();
        #$this->load->helper('url');
        $this->load->model('users_model');
    }

    public function index()
    {
        $data['user_list'] = $this->users_model->get_all_users();
        $this->load->view('show_users', $data);
    }

    public function add_form()
    {
        $this->load->view('insert');
    }

    public function insert_new_user()
    {
        $udata['Username'] = $this->input->post('name');
        $udata['Email-Id'] = $this->input->post('email');
        $udata['Address'] = $this->input->post('address');
        $udata['Mobile'] = $this->input->post('mobile');
        $res = $this->users_model->insert_users_to_db($udata);

        if($res)
        {
            header("location: http://localhost/crudcode/index.php/users_model/insert_users_to_db");
        }
        else
        {
            echo "Hello";
        }
    }

}

模型 users_model.php:

<?php

class Users_model extends CI_Model 
{
    function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

    public function get_all_users()
    {
        $query = $this->db->get('users');
        return $query->result();
    }

    public function insert_users_to_db($udata)
    {
        return $this->db->insert('users', $udata);
    }

    public function getById($id)
    {
        $query = $this->db->get_where('users',array('id'=>$id));
        return $query->row_array();
    }

}

?>

查看insert.php:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>CI Insert Form</title>
</head>
<body>
<form method="post" action="localhost/crudcode/index.php/users/insert_new_user">
<table width="400" border="0" cellpadding="5">

    <tr>
        <th width="213" align="right" scope="row">Enter your username</th>
        <td width="161"><input type="text" name="name" size="20" /></td>
    </tr>

    <tr>
        <th align="right" scope="row">Enter your email</th>
        <td><input type="text" name="email" size="20" /></td>
    </tr>

    <tr>
        <th align="right" scope="row">Enter your Mobile</th>
        <td><input type="text" name="mobile" size="20" /></td>
    </tr>

    <tr>
        <th align="right" scope="row">Enter Your Address</th>
        <td><textarea name="address" rows="5" cols="20"></textarea></td>
    </tr>

    <tr>
        <th align="right" scope="row">&nbsp;</th>
        <td><input type="submit" name="submit" value="Send" /></td>
    </tr>

</table>
</form>
</body>
</html>

Here is the error

最佳答案

您是否检查过表单中的数据是否已提交?喜欢

public function insert_new_user()
{
    print_r($this->input->post());
}

并检查您传递给用户模型的数组以及最后一个查询。

public function insert_users_to_db($udata)
{
    $this->db->insert('users', $udata);
    print_r($udata);
    echo $this->db->last_query();    
}

关于php - 无法使用codeigniter在Mysql中插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35082319/

相关文章:

mysql - MySQL 中何时使用单引号、双引号和反引号

mysql - 如何在 Joomla/mysql 中删除垃圾邮件用户

mysql - 在 MySql 中删除未命名的外键

php - 使用 PHP 的 JavaScript atob 操作

javascript - 检索 JSON 数据未按预期工作

php - 我应该使用存储过程来执行复杂的 SELECT 查询吗?

php - 限制php中获取数据的列

php - MySQL:规范化数据库的子查询

mysql - MySQL 的锁定和并发

mysql - 如何使用 Union 组合这两个查询?