How to store array data in MySQL database using PHP & MySQL?
I'm new to php and mysql and I'm trying to learn how to store the following array data from three different arrays friend[], hair_type[], hair_color[]
using MySQL and PHP an example would be nice. Thanks
Here is the HTML code.
<input type="text" name="friend[]" id="friend[]" />
<select id="hair_type[]" name="hair_type[]">
<option value="Hair Type" selected="selected">Hair Type</option>
<option value="Straight">Straight</option>
<option value="Curly">Curly</option>
<option value="Wavey">Wavey</option>
<option value="Bald">Bald</option>
</select>
<select id="hair_color[]" name="hair_color[]">
<option value="Hair Color" selected="selected">Hair Color</option>
<option value="Brown">开发者_JS百科Brown</option>
<option value="Black">Black</option>
<option value="Red">Red</option>
<option value="Blonde">Blonde</option>
</select>
<input type="text" name="friend[]" id="friend[]" />
<select id="hair_type[]" name="hair_type[]">
<option value="Hair Type" selected="selected">Hair Type</option>
<option value="Straight">Straight</option>
<option value="Curly">Curly</option>
<option value="Wavey">Wavey</option>
<option value="Bald">Bald</option>
</select>
<select id="hair_color[]" name="hair_color[]">
<option value="Hair Color" selected="selected">Hair Color</option>
<option value="Brown">Brown</option>
<option value="Black">Black</option>
<option value="Red">Red</option>
<option value="Blonde">Blonde</option>
</select>
Here is the MySQL tables below.
CREATE TABLE friends_hair (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
hair_id INT UNSIGNED NOT NULL,
user_id INT UNSIGNED NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE hair_types (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
friend TEXT NOT NULL,
hair_type TEXT NOT NULL,
hair_color TEXT NOT NULL,
PRIMARY KEY (id)
);
Create a table for hair type, hair color, and friend.
Hair type and hair color each need a primary key of id
, and a name
field.
Friend needs a primary key of id
, and fields of name
, hair_type_id
, and hair_color_id
.
Make the values for hair type and hair color their respective id from the database.
When you submit the form, loop through the $_POST['friend'] array, insert into the friend database the friend name, hair type id, and hair color id.
Don't forget to sanitize the inputs. Check out mysql_real_escape_string()
for an example of that.
I can make this more detailed if you need further help.
assuming that you can only have one hair type and one hair colour per user then check the pastie for a simple answer: http://pastie.org/928039
let's face it you can't have wavey and straight hair at the same time now can you ? Colours might be different in which case it's a many to many so you need a user_hair_colour table user_hair_colour(user_id PK, hair_colour_id PK) :P
select
u.*,
hc.name as hair_colour_name,
ht.name as hair_type_name
from
users u
inner join hair_colour hc on u.hair_colour_id = hc.hair_colour_id
inner join hair_type ht on u.hair_type_id = ht.hair_type_id
order by
u.username;
you can use the Serialise function to store the array into mysql database, and to retrieve the values use Unserialise function.. PHP serialize function
精彩评论