tqla Posted January 24, 2008 Share Posted January 24, 2008 Hello, I am using this code to print out an array and it works: foreach ($leads as $lead){ echo $lead; } I get a list of the $leads array. It looks like this "RedBlueYellowOrange" Then I attempt to place that array into my DB like this: $today = date("Y-m-d"); $date=time(); $sql = "INSERT INTO DB (createDate, leads ) VALUES ('$today', '$lead', )"; The problem is that only the last item in the array is placed in the database (Orange). How do I get them all to go into the DB? What am I doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/87486-solved-foreach-question/ Share on other sites More sharing options...
cooldude832 Posted January 24, 2008 Share Posted January 24, 2008 cause I don't see you querying in the loop <?php foreach($lead as $lead){ $q = "Insert into `DB` (createDate,leads) VALUES('".time()."','".$lead."')"; $r = mysql_query($q) or die(mysql_error()."<br /><br />".$q); } ?> try that Quote Link to comment https://forums.phpfreaks.com/topic/87486-solved-foreach-question/#findComment-447468 Share on other sites More sharing options...
tqla Posted January 24, 2008 Author Share Posted January 24, 2008 Actually I am querying it. I just left it out in my post. I am using mysql_query($sql) or die(mysql_error()); The problem is the only the last array item is going into the database. Just Orange. I would think that RedBlueYellowOrange would populate the DB. I don't get it. Quote Link to comment https://forums.phpfreaks.com/topic/87486-solved-foreach-question/#findComment-447482 Share on other sites More sharing options...
cooldude832 Posted January 24, 2008 Share Posted January 24, 2008 do what I gave you, echoing it won't make it appear in the query and if your only getting it to go once its pretty obvious its happen after the foreach loop Quote Link to comment https://forums.phpfreaks.com/topic/87486-solved-foreach-question/#findComment-447486 Share on other sites More sharing options...
tqla Posted January 24, 2008 Author Share Posted January 24, 2008 Ah ha. Thank you. I see. Okay, I did it but now instead of the 1 DB record showing "RedBlueYellowOrange", four records were created! One for Red, one for Blue, one for Yellow, and one for Orange. I want 1 record to be created and have "RedBlueYellowOrange" in the "leads" field. ??? Quote Link to comment https://forums.phpfreaks.com/topic/87486-solved-foreach-question/#findComment-447497 Share on other sites More sharing options...
cooldude832 Posted January 24, 2008 Share Posted January 24, 2008 the better way then is to do this <?php $lead = implode("",$leads); $q = "Insert into `DB` (createDate,leads) VALUES('".time()."','".$lead."')"; $r = mysql_query($q) or die(mysql_error()."<br /><br />".$q); ?> Don't see why you would ever want a single field like this want to explain what this is for? Quote Link to comment https://forums.phpfreaks.com/topic/87486-solved-foreach-question/#findComment-447499 Share on other sites More sharing options...
tqla Posted January 24, 2008 Author Share Posted January 24, 2008 Implode! I was thinking about that but had no idea how do properly use it. Don't see why you would ever want a single field like this want to explain what this is for? lol, actually this is just an excerpt of the real code. I extracted it so that I can find help here. Thanks cooldude832, I'll try this now. Quote Link to comment https://forums.phpfreaks.com/topic/87486-solved-foreach-question/#findComment-447532 Share on other sites More sharing options...
Barand Posted January 24, 2008 Share Posted January 24, 2008 @tqla see http://dev.mysql.com/tech-resources/articles/intro-to-normalization.html Quote Link to comment https://forums.phpfreaks.com/topic/87486-solved-foreach-question/#findComment-447608 Share on other sites More sharing options...
tqla Posted January 24, 2008 Author Share Posted January 24, 2008 Thanks cooldude832! Implode works perfectly. Barand, thank you too, I've put this on my study list. Quote Link to comment https://forums.phpfreaks.com/topic/87486-solved-foreach-question/#findComment-448059 Share on other sites More sharing options...
cooldude832 Posted January 24, 2008 Share Posted January 24, 2008 Thanks cooldude832! Implode works perfectly. Barand, thank you too, I've put this on my study list. It shouldn't be your "study" list it should be on your "OMG if I don't implement this correct now I will have a ton more work to do later" list. Quote Link to comment https://forums.phpfreaks.com/topic/87486-solved-foreach-question/#findComment-448213 Share on other sites More sharing options...
Barand Posted January 24, 2008 Share Posted January 24, 2008 OMG, we agree Quote Link to comment https://forums.phpfreaks.com/topic/87486-solved-foreach-question/#findComment-448220 Share on other sites More sharing options...
cooldude832 Posted January 24, 2008 Share Posted January 24, 2008 i am just as surprised as you are Quote Link to comment https://forums.phpfreaks.com/topic/87486-solved-foreach-question/#findComment-448324 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.