【问题标题】:how to insert a value to a foreign key using session without overriding the other sessions in codeigniter?如何使用会话将值插入外键而不覆盖codeigniter中的其他会话?
【发布时间】:2015-03-04 19:55:22
【问题描述】:

我希望你一切都好。有人可以帮我解决我的问题吗?我有 2 张桌子。另一个是针对客户的,它具有customer_id 的自动增量值。另一个表用于订单,它的orders_id 和另一个表(customers)的外键也有一个自动增量。

当我插入新客户时,如果成功,我希望页面重定向到添加新订单页面。在插入新订单时,我的orders 表中的customer_id 字段应该与新添加的客户具有相同的值。添加客户和添加新订单在我的控制器中具有不同的功能。我在插入新订单时遇到错误 1452,这意味着为 orders 表中的外键 customers_id 插入的值与另一个表(客户)中的值不同。

现在,我已经使用 session.我的问题是获取最后一个 id 的另一个会话覆盖了登录会话。

这是来自我的控制器的一些代码 sn-ps:

Class MyController extends CI_Controller
{	
	function __construct()
	{
		parent::__construct();
		$this->c_id = 0;
		if($this->session->userdata('logged_in'))
   		{
   			$session_data = $this->session->userdata('logged_in');
		    $data['username'] = $session_data['username'];
			if($session_data['username'] == 'administrator'){
				$this->load->database('sample');
				$this->load->model('samplemodel_model');
				$this->load->library('form_validation');
			} else {
   				redirect('home', 'refresh');
			}

		} else {
			redirect('login', 'refresh');
		}
	}
        public function index() {

		if($this->session->userdata('logged_in'))
   		{
            $session_data = $this->session->userdata('logged_in');
		    $data['username'] = $session_data['username'];

    		//code for validation here

			$customers = $this->samplemodel_model->get_entries('customers');

	   		if($this->form_validation->run() == FALSE) {
	    		//Field validation failed.
	   		} else {
	     		//Insert $data
                //$data = array('xxxxxx'); 
                //data is something like that
				$this->create($data);
	   		}
   		}
   		else
   		{
     		//If there's no session it will redirect to login page
   		}

	}

	//add new orders
	public function addOrders() {
		if($this->session->userdata('last_inserted_id')) //if I use this session, I can get the last inserted ID but the session data for the login will not be retrieved.
   		{
    		$session_data = $this->session->userdata('last_inserted_id');

			$orders = $this->samplemodel_model->get_entries('orders');

	   		if($this->form_validation->run() == FALSE) {
	    		//Field validation failed.
	   		} else {
	     		//Insert data

		    	$data = array('customer_id' => $session_data['customer_id'], 
		    				  'order_type' => $this->input->post('order_type'));

				$this->createItem($data);
	   		}
   		}
   		else
   		{
     		//If no session, redirect to login page
     		redirect('login', 'refresh');
   		}
	}

	//create customer
	public function create($data) {
     	//Insert data
		$customers = $this->samplemodel_model->get_entries('customers');

		//$data = array(xxxxx);
        //somethin' like that for data array

		$this->load->samplemodel_model->create('customers', $data);

		//***********************************************************//
		//                get and save last id inserted              //
		//***********************************************************//

 		//query the database
   		$result = $this->samplemodel_model->get_last_inserted($this->db->insert_id());
   		if($result)
	    {
	    	$sess_array = array();
	     	foreach($result as $row)
	     	{
	       		$sess_array = array('customer_id' => $row->customer_id);
	        	$this->session->set_userdata('last_inserted_id', $sess_array);
	     	}
	     	return TRUE;
	   	}
	   	else
	   	{
	   		echo "<script type='text/javascript'>alert('error');</script>";
	    	return false;
	   	}

	   	session_start('last_inserted_id');

	   	//********************************************************//
	   	//                          end                           //
	   	//********************************************************// 

		redirect('myController/addOrders', 'refresh');
     		
	}
	public function createItem($data) {
		//Insert data
		$orders = $this->samplemodel_model->get_entries('orders');

		$data = array('customer_id' => $session_data['customer_id'],
					  'order_type' => $this->input->post('order_type'));
			
		$this->load->samplemodel_model->create('orders', $data);

        //I'm not so sure if it is in this function that I should place the unset for the session 'last_inserted_id'

		redirect('home', 'refresh');
	}
}

在我的模型中,我插入了另一个函数来帮助我保存最后插入的 id。就是这样:

public function get_last_inserted($id)
	{
	   $this -> db -> select('customer_id');
	   $this -> db -> from('customers');
	   $this -> db -> where('customer_id', $id);
	   $this -> db -> limit(1);
	 
	   $query = $this -> db -> get();
	 
	   if($query -> num_rows() == 1)
	   {
	     return $query->result();
	   }
	   else
	   {
	     return false;
	   }
	}

请!帮助:'(如果您有任何其他想法,我将不胜感激。非常感谢您!

【问题讨论】:

    标签: php mysql database codeigniter


    【解决方案1】:

    问题在于您正在重定向,每个 HTTP 请求都是具有自己变量的自己的进程,并且每个请求都无法访问其他请求中设置的变量。

    尝试将客户 ID 作为参数传递给 addOrders(),然后可以使用 codeigniter 方式传递参数:

    http://www.example.com/controller/method/paramter

    查看文档:

    https://ellislab.com/codeigniter/user-guide/general/controllers.html

    在段下:将 URI 段传递给您的函数

    其他可能的解决方案:将 customerID 存储在会话中,或者在创建新用户时实例化的用户对象中,但这更多地取决于用例。

    【讨论】:

    • 我已经尝试过使用您的第一个解决方案,它对我不起作用。我的意思是,我想我只是不知道如何正确使用它(呵呵)。我现在正在尝试使用会话。我已经获得了 ID,但是在我使用会话时出现了一点问题。但无论如何,非常感谢您的回答。非常感谢。
    • 好吧,如果您想编辑您的问题并解释我(或其他人)可能会进一步解释的问题是什么
    • 我在使用会话时遇到了这个小问题。用于获取插入的最后一个 id 的另一个会话会覆盖用于登录的会话。 (捂脸)
    • 更新您的问题,因为我没有看到您将客户 ID 存储到任何会话中:)
    • 你好@Patrick,我已经编辑了这个问题。我添加了我不久前尝试过的新行。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-25
    • 1970-01-01
    • 1970-01-01
    • 2012-05-28
    • 1970-01-01
    • 2016-06-19
    • 1970-01-01
    相关资源
    最近更新 更多