简体   繁体   English

使用MySQL和PHP将数据存储在MySQL数据库中

[英]Storing data in a MySQL database using MySQL & PHP

I'm new to PHP and MySQL and I'm trying to store a users entered data from the following fields $skill, $experience, $years which a user can also add additional fields of $skill, $experience, $years if needed so in instead of 1 of each field there might be multiples of each field. 我是新来的PHP和MySQL和我试图存储用户从以下字段中输入的数据$skill, $experience, $years ,用户还可以添加额外的领域$skill, $experience, $years如果需要的话因此,每个字段可能有多个,而不是每个字段一个。

I was wondering how can I store the fields in my MySQL database using PHP and MySQL? 我想知道如何使用PHP和MySQL将字段存储在MySQL数据库中? I have the following script but I know its wrong. 我有以下脚本,但我知道它是错误的。 can some one help me fix the script listed below? 有人可以帮我修复下面列出的脚本吗?

Here is the PHP and MySQL code. 这是PHP和MySQL代码。

$skill = serialize($_POST['skill']);
$experience = serialize($_POST['experience']);
$years = serialize($_POST['years']);


for (($s = 0; $s < count($skill); $s++) && ($x = 0; $x < count($experience); $x++) && ($g = 0; $g < count($years); $g++)){
    $mysqli = mysqli_connect("localhost", "root", "", "sitename");
    $query1 = "INSERT INTO learned_skills (skill, experience, years) VALUES ('" . $skill[$s] . "', '" . $experience[$x] . "', '" . $years[$g] . "')";

    if (!mysqli_query($mysqli, $query1)) {
            print mysqli_error($mysqli);
            return;
    }

}

Here is my MySQL table. 这是我的MySQL表。

CREATE TABLE learned_skills (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
skill TEXT NOT NULL,
experience TEXT NOT NULL,
years INT NOT NULL,
PRIMARY KEY (id)
);

CREATE TABLE u_skills (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
skill_id INT UNSIGNED NOT NULL, 
users_id INT UNSIGNED NOT NULL,
PRIMARY KEY (id)
);

You would create two tables that have a 1 to many relationship: 您将创建两个具有一对多关系的表:

CREATE TABLE `users` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `username` varchar(40) collate latin1_general_ci NOT NULL,
  `password` varchar(255) collate latin1_general_ci NOT NULL
  PRIMARY KEY  (`id`),
  UNIQUE KEY `username` (`username`)
);



CREATE TABLE learned_skills (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
user_id INT UNSIGNED NOT NULL,
skill TEXT NOT NULL,
experience TEXT NOT NULL,
years INT NOT NULL,
PRIMARY KEY (id)
);

This way a user can have any number of listed skills. 这样,用户可以拥有许多列出的技能。 If skill is really just a small text string (like "PHP" or "MySQL") then you should use a VARCHAR type instead of TEXT. 如果Skill实际上只是一个很小的文本字符串(例如“ PHP”或“ MySQL”),则应使用VARCHAR类型而不是TEXT。 If that's the case, once you get going you'll see that it would be better to create a list of skills that the user can choose from and just have a skill_id rather than a text field. 如果真是这样,那么一旦开始学习,您会发现创建一个用户可以选择的技能列表更好,并且只具有一个skill_id而不是文本字段会更好。 This helps with something called normalization (a way to prevent duplicate data). 这有助于进行标准化(一种防止重复数据的方法)。

Good luck in your learning. 祝您学习顺利。

If there are multiples, you will have to either create a column for each one or put them into the same field with some sort of a delimiter (skill1, skill2, skill3). 如果有多个,则必须为每个数字创建一列,或者使用某种分隔符(skill1,skill2,skill3)将它们放在同一字段中。 Then, upon retrieval you can split the array using that delimiter. 然后,在检索时,您可以使用该定界符拆分数组。

you can use multiple tables with column lookups as suggested. 您可以按照建议将多个表与列查找一起使用。

there are also times when you might want to use a serialized array like you started to do. 有时候,您可能想像开始那样使用序列化数组。 however, after you serialized it, it is a string, so trying to access values by index does not make sense. 但是,序列化后,它是一个字符串,因此尝试按索引访问值没有意义。 you have 你有

 $skill = serialize($_POST['skill']);
...    

    $query1 = "INSERT INTO learned_skills (skill, experience, years) 
VALUES ('" $skill[$s] 

which would not work. 这是行不通的。

also you might want to do something like 你也可能想做类似的事情

$counter = min(count($skill), count($experience), count($years));
for($i = 0; $i < $counter; ++$i){

instead of 代替

for (($s = 0; $s < count($skill); $s++) && ($x = 0; $x < count($experience); $x++) && ($g = 0; $g < count($years); $g++)){

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

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