2016-01-29 87 views
1

由於數據庫錯誤1048,我無法將數據插入到mysql中。我一直在搜索近1周,但找不到任何解決方案。請幫幫我。這裏是所有的東西...無法使用codeigniter在Mysql中插入數據

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

+0

我已經這樣做了,但是。它只顯示空白頁,沒有數據被添加到表中,但錯誤1048已不存在@saty –

+0

Print_r($ _ POST); CHK什麼我得到 – devpro

+0

好吧,這個問題已經解決了。我認爲實際的問題是我不會將數據從表單發送到控制器。解決了。非常感謝您的幫助:) –

回答

0

我假設你沒有提交空表格。 問題可能是發生了重定向,它不包含$ POST數據。

您是否嘗試過使用此作爲一個動作(與右尾/):

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

我相信Email-Id是無效的字段名。如果您有一個名爲字段的方式,你必須逃脫它喜歡:

`Email-Id` 

或者更準確地說:

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

雖然一個更好的解決辦法是重新命名的領域,因爲這是連命名約定。只使用CamelCase或只使用underscore_names。對於任何我知道的語言中的字段/變量名稱,破折號不是有效的字符。

+0

爲什麼Email-Id是無效名稱? – bestprogrammerintheworld

+0

,因爲它包含一個'-'。這是一個在SQL和幾乎所有語言中都具有語法意義的符號。 – coladict

+0

這根本不是真的。可以像這樣重命名字段列。無論如何,在MySQL中。 – bestprogrammerintheworld

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

您打給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); 

通常說來,你不應該需要/不應該重定向到模型。這些重定向應該由控制器處理。

+0

它顯示相同的錯誤,更改代碼後沒有發生任何事 –

+0

數組$ udata的值是什麼?只需var_dump($ udata)並暫時移除插入函數。 – bestprogrammerintheworld

+0

好吧,它顯示數組的值也爲null。 –

1

你可以從

<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" > 
+0

是啊,我已經嘗試過,但有一個調用base_url()的問題.. –

+0

不,我仍然受苦,同樣的錯誤正在顯示.. –

0

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

2)檢查您要插入的數組。列名稱應該與assoc數組相匹配。

相關問題