i'm using codeigniter json items database , insert them. created following tutorial on codeigniter site, uses form send data database, , worked correctly. i'm attempting amend code can write database putting field values in url string. so, data, it's http://www.mcbiscuit.com/index.php/robots/. calls robots controller.
you can put http://www.mcbiscuit.com/robots/(name) see database entry robot. need fix set method can use url http://www.mcbiscuit.com/robots/create/(name)/(starsin)/(function) write entry these details database, whatever do, passes null values these. how make insert records correctly? i've read can writing databases php, , codeigniter api calls , such, , cannot find solution.
controller code:
<?php class robots extends ci_controller { public function __construct() { parent::__construct(); $this->load->model('robot_model'); $this->load->helper('url_helper'); } public function index() { header('content-type: application/json'); echo json_encode($this->robot_model->get_robots(), json_pretty_print); //$data['robots'] = $this->robot_model->get_robots(); //$data['title'] = 'robots'; $this->load->view('robots/index'); } public function view($name = null) { header('content-type: application/json'); echo json_encode($this->robot_model->get_robots($name), json_pretty_print); $this->load->view('robots/view'); } public function create($name, $starsin, $function) { $this->robot_model->set_robots($name, $starsin, $function); $this->load->view('robots/success'); } }
model code:
<?php class robot_model extends ci_model { public function __construct() { $this->load->database(); } public function get_robots($name = false) { if ($name === false) { $query = $this->db->get('robots'); return $query->result_array(); } $query = $this->db->get_where('robots', array('name' => $name)); return $query->row_array(); } public function set_robots($name, $starsin, $function) { //$data = parse_str($_server['query_string'], $_get); $data = array( 'name' => $this->input->post('name'), 'starsin' => $this->input->post('starsin'), 'function' => $this->input->post('function') ); var_dump($data); return $this->db->set('robots', $data); } }
routing code, because i'm not sure correct:
$route['robots/create'] = 'robots/create'; $route['robots/(:any)'] = 'robots/view/$1'; $route['robots'] = 'robots'; $route['(:any)'] = 'pages/view/$1'; $route['default_controller'] = 'pages/view';
yeah routing robots/create
incorrect, should be
$route['robots/create/(:any)/(:any)/(:any)'] = 'robots/create/$1/$2/$3';
this way codeigniter knows after create/
considered get/method parameters.
but shouldn't using parameters insert/update/delete records, because example post link/picture somewhere on website , put redirect link robots/create/some/idiotic/data
, logged in website clicks link/picture insert rubbish data database.
google csrf attack learn more , how protect against it.
also looking @ set_robots
method, trying create $data
array $this->input->post()
not using post array should be:
$data = array( 'name' => $name, 'starsin' => $starsin, 'function' => $function );