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
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.

Link to comment
Share on other sites

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.  ???

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.