Jump to content

[SOLVED] Foreach question


tqla

Recommended Posts

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?

 

 

Link to comment
https://forums.phpfreaks.com/topic/87486-solved-foreach-question/
Share on other sites

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.

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.  :o  I want 1 record to be created and have "RedBlueYellowOrange" in the "leads" field.  ???

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?

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.