【发布时间】: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"> </th>
<td><input type="submit" name="submit" value="Send" /></td>
</tr>
</table>
</form>
</body>
</html>
【问题讨论】:
-
我已经这样做了,但是。它只显示空白页,没有数据添加到表中,但错误 1048 消失了@saty
-
Print_r($_POST); CHK 你得到了什么
-
好的,问题已经解决了。我认为实际的问题是我无法将数据从表单发送到控制器。这样就解决了。非常感谢您的帮助:)
标签: php mysql database codeigniter