简体   繁体   English

如何使用PHP和MySQL在MySQL数据库中存储数组数据?

[英]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. 我是php和mysql的新手,我试图学习如何使用MySQL和PHP从三个不同的数组friend[], hair_type[], hair_color[]存储以下数组数据,一个示例很好。 Thanks 谢谢

Here is the HTML code. 这是HTML代码。

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





<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. 这是下面的MySQL表。

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. 头发类型和头发颜色每个都需要id的主键和一个name字段。

Friend needs a primary key of id , and fields of name , hair_type_id , and hair_color_id . Friend需要一个主键id ,以及name字段, hair_type_idhair_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. 提交表单时,遍历$ _POST ['friend']数组,将朋友名称,头发类型ID和头发颜色ID插入朋友数据库。

Don't forget to sanitize the inputs. 不要忘了清理输入。 Check out mysql_real_escape_string() for an example of that. 查看mysql_real_escape_string()的示例。

I can make this more detailed if you need further help. 如果您需要进一步的帮助,我可以做得更详细。

您可以使用序列化功能来存储阵列到MySQL数据库和检索的值,用Unserialise功能.. PHP序列化功能

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 假设每个用户只能拥有一种头发类型和一种头发颜色,则可以检查Pastie的简单答案: 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 在这种情况下,颜色可能会有所不同,因此您需要一个user_hair_colour表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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM