i can not insert data mysql because of database error 1048.i have been searching 1 week, couldn't find solution. please me. here 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 username</th> <td width="161"><input type="text" name="name" size="20" /></td> </tr> <tr> <th align="right" scope="row">enter email</th> <td><input type="text" name="email" size="20" /></td> </tr> <tr> <th align="right" scope="row">enter mobile</th> <td><input type="text" name="mobile" size="20" /></td> </tr> <tr> <th align="right" scope="row">enter 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>
have check if data form submitted?
public function insert_new_user() { print_r($this->input->post()); }
and check array you've pass on user model , last query well.
public function insert_users_to_db($udata) { $this->db->insert('users', $udata); print_r($udata); echo $this->db->last_query(); }
Comments
Post a Comment