serbestgezer Posted October 1, 2009 Share Posted October 1, 2009 Hi There, I am trying to get session cart values into order_items table <?php $cart = $_SESSION[cart]; $a = array($cart); //echo $cart; foreach($a as $data) { echo $data; // 1,1,1,1 //$sql = mysql_query("insert into order_items (code) VALUES ('$data')") or die(mysql_error()); } ?> I dont know what i am doing wrong. it enters the data like 1,1,1,1 doesnt repeats the insert I want it like that order_items table id code 1 1 2 1 3 1 4 1 How can I get the session values and insert them into database? Link to comment https://forums.phpfreaks.com/topic/176130-solved-session-values-into-mysql/ Share on other sites More sharing options...
trq Posted October 1, 2009 Share Posted October 1, 2009 foreach($_SESSION['cart'] as $data) Link to comment https://forums.phpfreaks.com/topic/176130-solved-session-values-into-mysql/#findComment-928096 Share on other sites More sharing options...
serbestgezer Posted October 1, 2009 Author Share Posted October 1, 2009 I tried it before and got error message Warning: Invalid argument supplied for foreach() Link to comment https://forums.phpfreaks.com/topic/176130-solved-session-values-into-mysql/#findComment-928101 Share on other sites More sharing options...
trq Posted October 1, 2009 Share Posted October 1, 2009 What is the output of.... print_r($_SESSION['cart']); Link to comment https://forums.phpfreaks.com/topic/176130-solved-session-values-into-mysql/#findComment-928118 Share on other sites More sharing options...
redarrow Posted October 1, 2009 Share Posted October 1, 2009 session's dont work without session_start() Link to comment https://forums.phpfreaks.com/topic/176130-solved-session-values-into-mysql/#findComment-928126 Share on other sites More sharing options...
redarrow Posted October 1, 2009 Share Posted October 1, 2009 look at this example please of session's <?php session_start(); $data=array(1,1,1,1); foreach($data as $data_info){ $_SESSION['cart']=$data_info; echo $_SESSION['cart']; }?> Link to comment https://forums.phpfreaks.com/topic/176130-solved-session-values-into-mysql/#findComment-928131 Share on other sites More sharing options...
serbestgezer Posted October 1, 2009 Author Share Posted October 1, 2009 print_r($_SESSION['cart']); output 1,1,1,1,1 Link to comment https://forums.phpfreaks.com/topic/176130-solved-session-values-into-mysql/#findComment-928132 Share on other sites More sharing options...
serbestgezer Posted October 1, 2009 Author Share Posted October 1, 2009 session's dont work without session_start() I know mate. I do use session_start Link to comment https://forums.phpfreaks.com/topic/176130-solved-session-values-into-mysql/#findComment-928134 Share on other sites More sharing options...
trq Posted October 1, 2009 Share Posted October 1, 2009 <?php $cart = implode(',', $_SESSION[cart]); foreach($cart as $data) { mysql_query("insert into order_items (code) VALUES ('$data')") or die(mysql_error()); } ?> Link to comment https://forums.phpfreaks.com/topic/176130-solved-session-values-into-mysql/#findComment-928139 Share on other sites More sharing options...
serbestgezer Posted October 1, 2009 Author Share Posted October 1, 2009 I changed the code as below. $cart = $_SESSION['cart']; $items = explode(',',$cart); $contents = array(); foreach ($items as $item => $value) { print_r($contents[$value]); } doesnt print anything but if I use print_r($contents[$value] . "test "); it prints //test test test test I dont get the session value ???? Link to comment https://forums.phpfreaks.com/topic/176130-solved-session-values-into-mysql/#findComment-928141 Share on other sites More sharing options...
redarrow Posted October 1, 2009 Share Posted October 1, 2009 thorpe, is there any reason there no session_start() on the top of your example.... is it not needed in this example off work? things change so quickly maybe session_start() not needed after all Link to comment https://forums.phpfreaks.com/topic/176130-solved-session-values-into-mysql/#findComment-928142 Share on other sites More sharing options...
serbestgezer Posted October 1, 2009 Author Share Posted October 1, 2009 ok here we go. <?php session_start(); // for redarrow $cart = explode(',', $_SESSION[cart]); foreach($cart as $data) { mysql_query("insert into order_items (code) VALUES ('$data')") or die(mysql_error()); } ?> thanks guys Link to comment https://forums.phpfreaks.com/topic/176130-solved-session-values-into-mysql/#findComment-928143 Share on other sites More sharing options...
serbestgezer Posted October 1, 2009 Author Share Posted October 1, 2009 redarrow, I do use session_start(). i didnt post it the whole script. its just part of it. Link to comment https://forums.phpfreaks.com/topic/176130-solved-session-values-into-mysql/#findComment-928144 Share on other sites More sharing options...
redarrow Posted October 1, 2009 Share Posted October 1, 2009 So cart was a string $cart="1,1,1,1"; <<< you never said that? Link to comment https://forums.phpfreaks.com/topic/176130-solved-session-values-into-mysql/#findComment-928146 Share on other sites More sharing options...
serbestgezer Posted October 1, 2009 Author Share Posted October 1, 2009 no cart is not a string. it was ex. from my shopping cart output. Link to comment https://forums.phpfreaks.com/topic/176130-solved-session-values-into-mysql/#findComment-928148 Share on other sites More sharing options...
redarrow Posted October 1, 2009 Share Posted October 1, 2009 according to your code if it goes like this. then the $cart << was a string no matter where it come from.... <?php session_start(); $_SESSION['cart']="1,1,1,1"; $cart = explode(',', $_SESSION['cart']); foreach($cart as $data) { echo $data; } ?> result. 1111 Link to comment https://forums.phpfreaks.com/topic/176130-solved-session-values-into-mysql/#findComment-928149 Share on other sites More sharing options...
serbestgezer Posted October 2, 2009 Author Share Posted October 2, 2009 you added it. $_SESSION['cart']="1,1,1,1"; not me. Link to comment https://forums.phpfreaks.com/topic/176130-solved-session-values-into-mysql/#findComment-928801 Share on other sites More sharing options...
trq Posted October 2, 2009 Share Posted October 2, 2009 print_r indicated you indeed had a string, not an array. Link to comment https://forums.phpfreaks.com/topic/176130-solved-session-values-into-mysql/#findComment-928862 Share on other sites More sharing options...
serbestgezer Posted October 2, 2009 Author Share Posted October 2, 2009 i see, my print_r shows as Array[0] =>1[1]=>1 i think, what I was thinking and what I wrote was bit different Thanks Thorpe Link to comment https://forums.phpfreaks.com/topic/176130-solved-session-values-into-mysql/#findComment-928880 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.