Jump to content

[SOLVED] Inserting and Updating Arrays...


briant

Recommended Posts

I recently discovered arrays... when I say recent, I mean today, haha...

 

I've looked around and its incredible hard to find a tutorial on it.

 

Let's say this is my array:

$names = array("Brian","Billy","Bob","Joe");

 

How am I suppose to update my array?

mysql_query("UPDATE site_table SET friends = 'what do i put in here' WHERE user_id = 2");

 

Also, if I want to add another name to the array, how am I to do that?

 

Thanks in advance.

Link to comment
Share on other sites

So you want it to appear in the database in a text field as one line like so?

Brian Billy Bob Joe

 

If thats how you want it, then you would do this:

 

<?php

$names = array("Brian","Billy","Bob","Joe");

$query = "UPDATE site_table SET friends = '" . implode($names, " ") . "' WHERE user_id = 2"
$result = mysql_query($query)or die(mysql_error());

?>

 

Link to comment
Share on other sites

Thanks pocobueno1388 for your rapid responses.

 

OK, I think I might not have understood the use of arrays...

 

Am I still able to call upon the the friends field as an array? like $friends[3]? And it would show Bob.

 

Yes, you will still be able to call which name you want from the array by using the key like you did.

 

But Bob would be $friend[2] because the counter starts at 0...here is an example.

 

<?php

$names = array("Brian","Billy","Bob","Joe");
                  0       1      2     3

 

Does that make sense?

Link to comment
Share on other sites

Wow, awesome pocobueno1388... you are very knowledgeable.

 

One last thing and I'll leave you alone. Let's say instead of a " " space, since I might have a name that includes the last name "Bob Barker". I want all my array stored in the database with brackets.

 

For example:

[brian][billy][bob Barker][Joe][Cindy]

 

How am I suppose to explode that into an array?

Maybe it's this?

$friends = explode("[", $friends, "]");

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.