简体   繁体   English

无法使用Codeigniter在Mysql中插入数据

[英]Can not insert data in Mysql using codeigniter

I can not insert data into mysql because of database error 1048.I have been searching from almost 1 week, but i couldn't find any solution. 由于数据库错误1048,我无法将数据插入mysql。我已经搜索了将近1个星期,但找不到任何解决方案。 please help me. 请帮我。 Here is all the stuff... 这是所有的东西...

controller users.php: 控制器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";
        }
    }

}

Model users_model.php: 模型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();
    }

}

?>

view insert.php: 查看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 这是错误

Have you check if the data from the form is submitted? 您是否检查过表单中的数据是否已提交? like 喜欢

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

and check the array you've pass on the user model and the last query as well. 并检查您传递给用户模型的数组以及上一个查询。

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

You Can try with changing the FORM action from 您可以尝试通过以下方式更改FORM操作

<form method="post" action="localhost/crudcode/index.php/users/insert_new_user"> 

TO

<form method="post" action="<?php echo base_url();?>index.php/users/insert_new_user" >

I am assuming you are not submitting an empty form. 我假设您没有提交空白表格。 The problem could be that there is a redirect happening which is not carrying your $POST data. 问题可能是发生了重定向,该重定向未携带您的$ POST数据。

Have you tried using this as an action (with the right trailing /): 您是否尝试过将其用作操作(后跟正确的/):

http://localhost/crudcode/index.php/users/insert_new_user/

You're calling the insert_user_to_db() twice. 您要两次调用insert_user_to_db()

First with: 首先搭配:

$res = $this->users_model->insert_users_to_db($udata);

and in this call you're sending the values from the $udata array. 在此调用中,您将从$udata array.发送值$udata array.

And then with: 然后加上:

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

and this call does not include any arguments so it's probably in this call it fails. 并且此调用不包含任何参数,因此可能在此调用中失败。

What happens if you exclude the second call and just to like this? 如果您不打第二个电话而只是这样,会发生什么?

$res = $this->users_model->insert_users_to_db($udata);
echo print_r($res, true);

Genereally speaking you should not need/should not redirect to a model. 通常来说,您不需要/不应重定向到模型。 Those kind of redirects should be handled by the controller(s). 这类重定向应由控制器处理。

1)Better to use form_open() and form_close(). 1)最好使用form_open()和form_close()。

2)Check Array which you are inserting.Columns names should match with the assoc array. 2)检查要插入的数组。列名应与assoc数组匹配。

I believe Email-Id is an invalid field name. 我认为Email-Id是无效的字段名称。 If you have named a field that way, you must escape it like: 如果以这种方式命名了字段,则必须像下面这样对它进行转义:

`Email-Id`

or more accurately: 或更准确地说:

$udata['`Email-Id`'] = $this->input->post('email');

Though a much better solution is to rename the field, because that isn't even a naming convention. 尽管更好的解决方案是重命名字段,因为这甚至不是命名约定。 Either use only CamelCase or only underscore_names. 仅使用CamelCase或仅使用underscore_names。 A dash not a valid character for a field/variable name in any language I know of. 我知道的任何语言中,破折号都不是字段/变量名称的有效字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM