• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
insert query stalling when trying to pass data stored in session

#1
[eluser]Brad K Morse[/eluser]
I store a piece of data in session, then when I make an insert into the database, I pass that in, like this.

Code:
$data = array(
'age' => $this->session->userdata('age'),
'weight' => $this->input->post('weight')
);

$this->db->insert('table', $data);

It will stall with me passing the age stored in the session.

If I remove that and just past in a number, like below, it works without a problem

Code:
$data = array(
'age' => 20,
'weight' => $this->input->post('weight')
);

$this->db->insert('table', $data);

I echo'd out $this->session->userdata('age') and it displays the appropriate age.

I also tried inserting the value into a hidden input and inserting it into the table w/o directly calling it from session and it still stalls.

Any help is appreciated.

#2
[eluser]Cristian Gilè[/eluser]
Try to cast the session value:

Code:
$data = array(
'age' => (int)$this->session->userdata('age'),
'weight' => $this->input->post('weight')
);

$this->db->insert('table', $data);

#3
[eluser]Brad K Morse[/eluser]
No luck.

I did

Code:
print_r($this->session->all_userdata());

I even stored the data into a variable, then tried passing it into the insert query.

Code:
$value = $this->session->all_userdata();

$value['age'];

I still get the same problem.

I've used session data in past applications when passing it into a db query, it can't be permission errors as the data is stored in session and prints out whenever I call it.

#4
[eluser]Cristian Gilè[/eluser]
Quote:
Code:
$value = $this->session->all_userdata();

$value['age'];

Are you able to echo the age value?

#5
[eluser]Brad K Morse[/eluser]
I think the problem was the database table column was expecting a number and not a string. I changed the column type to varchar2 for now until I change the output of the session variable to an integer.

#6
[eluser]Cristian Gilè[/eluser]
Did you try the cast solution?

Code:
$data = array(
'age' => (int)$this->session->userdata('age'),
'weight' => $this->input->post('weight')
);

$this->db->insert('table', $data);

#7
[eluser]Brad K Morse[/eluser]
Thanks Cristian, (int) did the trick.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.