由於數據庫錯誤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"> </th>
<td><input type="submit" name="submit" value="Send" /></td>
</tr>
</table>
</form>
</body>
</html>
我已經這樣做了,但是。它只顯示空白頁,沒有數據被添加到表中,但錯誤1048已不存在@saty –
Print_r($ _ POST); CHK什麼我得到 – devpro
好吧,這個問題已經解決了。我認爲實際的問題是我不會將數據從表單發送到控制器。解決了。非常感謝您的幫助:) –