Adding a string into an array
Posted: Sat Jul 01, 2006 2:16 am
I know, I tend to glance over the basics, but I just can't seem to solve this problem.
The following db/table/record names have been modified for security reasons.
My database has a table called 'necklace'. One of the fields is color_id in which you can list an infinite number of 'color_id's
So for example:
Necklace Table
Now I have a php script that extracts this information from a database and adds an extra color_id depending on what color the user wants to add.
Can anyone help? Thanks!
The following db/table/record names have been modified for security reasons.
My database has a table called 'necklace'. One of the fields is color_id in which you can list an infinite number of 'color_id's
So for example:
Necklace Table
Code: Select all
Necklace_id | Name | Color_id
5 | Oxyde | 2,5,34,83,23Code: Select all
// Get the necklace details
$sql = "SELECT * FROM necklace WHERE necklace_id='5';
$result = mysql_query ($sql);
$color_array = explode(",", $result['color_id']);
// At this point, $color_array is an array, but I want to add the string $new_color into $color_array. Can't for the life of me figure out how to do this
$color_id = implode(",", $color_array);
// Update necklace record with new color ID.
$sql2 = "UPDATE necklace SET color_id='$color_id' WHERE necklace_id='5'";
$result2 = mysql_query ($sql2);