Reputation: 612
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. please help me. Here is all the stuff...
controller 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:
<?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:
<!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>
Upvotes: 1
Views: 1819
Reputation: 169
1)Better to use form_open() and form_close().
2)Check Array which you are inserting.Columns names should match with the assoc array.
Upvotes: 0
Reputation: 291
You Can try with changing the FORM action from
<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" >
Upvotes: 1
Reputation: 5520
You're calling the insert_user_to_db()
twice.
First with:
$res = $this->users_model->insert_users_to_db($udata);
and in this call you're sending the values from the $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).
Upvotes: 0
Reputation: 250
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();
}
Upvotes: 1
Reputation: 5095
I believe Email-Id
is an invalid field name. 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. A dash not a valid character for a field/variable name in any language I know of.
Upvotes: -1
Reputation: 1540
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.
Have you tried using this as an action (with the right trailing /):
http://localhost/crudcode/index.php/users/insert_new_user/
Upvotes: 0