【问题标题】:Can not insert data in Mysql using codeigniter无法使用codeigniter在Mysql中插入数据
【发布时间】:2016-01-29 10:34:36
【问题描述】:

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

控制器 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

【问题讨论】:

  • 我已经这样做了,但是。它只显示空白页,没有数据添加到表中,但错误 1048 消失了@saty
  • Print_r($_POST); CHK 你得到了什么
  • 好的,问题已经解决了。我认为实际的问题是我无法将数据从表单发送到控制器。这样就解决了。非常感谢您的帮助:)

标签: php mysql database codeigniter


【解决方案1】:

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

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();    
}

【讨论】:

    【解决方案2】:

    您可以尝试从

    更改 FORM 操作
    <form method="post" action="localhost/crudcode/index.php/users/insert_new_user"> 
    

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

    【讨论】:

      【解决方案3】:

      我假设您没有提交空表单。 问题可能是发生了未携带您的 $POST 数据的重定向。

      您是否尝试过将其用作操作(右尾 /):

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

      【讨论】:

        【解决方案4】:

        您拨打了两次insert_user_to_db()

        首先是:

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

        在此调用中,您将发送来自 $udata array. 的值

        然后用:

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

        而且这个调用不包含任何参数,所以它可能在这个调用中失败了。

        如果您排除第二个电话并只是喜欢这个会发生什么?

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

        一般来说,您不需要/不应该重定向到模型。此类重定向应由控制器处理。

        【讨论】:

        • 报同样的错误,改代码后没有任何反应
        • 那么数组 $udata 的值是多少?只需 var_dump($udata) 并暂时删除 insert-function。
        • 好的,数组的值也显示为空。
        • 那才是真正的问题。由于某种原因,您没有从表单中获得值。如果你尝试 echo $this->input->post('name');并回显 $_POST['name'];它显示什么?
        【解决方案5】:

        1)最好使用form_open()和form_close()。

        2)检查您要插入的数组。列名应与 assoc 数组匹配。

        【讨论】:

          【解决方案6】:

          我相信Email-Id 是无效的字段名称。如果您以这种方式命名了一个字段,则必须将其转义为:

          `Email-Id`
          

          或更准确地说:

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

          虽然更好的解决方案是重命名字段,因为这甚至不是命名约定。要么只使用 CamelCase,要么只使用 underscore_names。对于我所知道的任何语言的字段/变量名称,破折号都不是有效字符。

          【讨论】:

          • 为什么 Email-Id 是无效名称?
          • 因为它包含一个-。这是一个在 SQL 和几乎所有语言中具有句法意义的符号。
          • 这根本不是真的。可以像这样重命名字段列。在 MySQL 中无论如何都是这样。
          • 仅仅因为 MySQL 允许您在大多数情况下做错事,并不意味着它是正确的事。在否决之前,您是否尝试过我的建议?
          • “仅仅因为 MySQL 允许你在大多数时候做错事”基于什么?
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-06-01
          • 1970-01-01
          相关资源
          最近更新 更多