如何使用CodeIgniter的URL参数将项目插入数据库?

时间:2022-10-06 16:04:28

I'm using CodeIgniter to get JSON items from a database and also insert them. I originally created it following the tutorial on the CodeIgniter site, which uses a form to send data to the database, and that worked correctly. I'm attempting to amend this code so that you can write to the database by putting the field values in the URL string. So, to GET data, it's http://www.mcbiscuit.com/index.php/robots/. This calls the Robots controller.

我正在使用CodeIgniter从数据库中获取JSON项目并插入它们。我最初是根据CodeIgniter网站上的教程创建的,该网站使用表单将数据发送到数据库,并且工作正常。我正在尝试修改此代码,以便您可以通过将字段值放在URL字符串中来写入数据库。因此,要获取数据,请访问http://www.mcbiscuit.com/index.php/robots/。这称为机器人控制器。

You can also put http://www.mcbiscuit.com/robots/(Name) to see the database entry for that robot. I need to fix the set method so that you can use a URL like this http://www.mcbiscuit.com/robots/create/(Name)/(StarsIn)/(Function) to write an entry with these details to the database, but whatever I do, it passes NULL values for these. How do I make it insert the records correctly? I've read everything I can about writing to databases with PHP, and CodeIgniter API calls and such, and I cannot find a solution.

您还可以使用http://www.mcbiscuit.com/robots/(Name)查看该机器人的数据库条目。我需要修复set方法,以便您可以使用这样的URL http://www.mcbiscuit.com/robots/create/(Name)/(StarsIn)/(Function)来写入包含这些详细信息的条目到数据库,但无论我做什么,它都为这些传递NULL值。如何正确插入记录?我已经阅读了有关使用PHP写入数据库以及CodeIgniter API调用等的所有内容,但我无法找到解决方案。

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 this is 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';

1 个解决方案

#1


2  

Yeah routing for robots/create is incorrect, it should be

是的机器人/创建路由不正确,它应该是

$route['robots/create/(:any)/(:any)/(:any)'] = 'robots/create/$1/$2/$3';

This way codeigniter knows that anything after create/ are considered as GET/method parameters.

这样,codeigniter知道create /之后的任何内容都被认为是GET /方法参数。

But you really shouldn't be using GET parameters to insert/update/delete records, because for example I could post a link/picture somewhere on some website and put a redirect link to your robots/create/some/idiotic/data and anyone who is logged in your website that clicks my link/picture will insert rubbish data into your database.

但是你真的不应该使用GET参数来插入/更新/删除记录,因为例如我可以在某个网站上的某个地方发布链接/图片并将重定向链接放到你的机器人/创建/某些/ idiotic /数据和任何人谁登录您的网站点击我的链接/图片将垃圾数据插入您的数据库。

Google for CSRF attack to learn more about it and how to protect against it.

谷歌进行CSRF攻击以了解更多信息以及如何防范它。

Also looking at set_robots method, you are trying to create a $data array with $this->input->post() but you are not using POST so your array should just be:

另外看一下set_robots方法,你试图用$ this-> input-> post()创建一个$ data数组,但是你没有使用POST,所以你的数组应该只是:

$data = array(
    'Name'     => $name,
    'StarsIn'  => $starsIn,
    'Function' => $function
);

#1


2  

Yeah routing for robots/create is incorrect, it should be

是的机器人/创建路由不正确,它应该是

$route['robots/create/(:any)/(:any)/(:any)'] = 'robots/create/$1/$2/$3';

This way codeigniter knows that anything after create/ are considered as GET/method parameters.

这样,codeigniter知道create /之后的任何内容都被认为是GET /方法参数。

But you really shouldn't be using GET parameters to insert/update/delete records, because for example I could post a link/picture somewhere on some website and put a redirect link to your robots/create/some/idiotic/data and anyone who is logged in your website that clicks my link/picture will insert rubbish data into your database.

但是你真的不应该使用GET参数来插入/更新/删除记录,因为例如我可以在某个网站上的某个地方发布链接/图片并将重定向链接放到你的机器人/创建/某些/ idiotic /数据和任何人谁登录您的网站点击我的链接/图片将垃圾数据插入您的数据库。

Google for CSRF attack to learn more about it and how to protect against it.

谷歌进行CSRF攻击以了解更多信息以及如何防范它。

Also looking at set_robots method, you are trying to create a $data array with $this->input->post() but you are not using POST so your array should just be:

另外看一下set_robots方法,你试图用$ this-> input-> post()创建一个$ data数组,但是你没有使用POST,所以你的数组应该只是:

$data = array(
    'Name'     => $name,
    'StarsIn'  => $starsIn,
    'Function' => $function
);