简体   繁体   English

使用多个类别和复选框更新SQL(PHP)

[英]Updating SQL with multiple categories and checkbox (PHP)

I have table of category like this 我有这样的类别表

Table Name : personality 表名:个性

 id | Category
  1 | cute
  2 | rich
  3 | stupid
  4 | funny
  5 | famous

All i want to do is make to check box option with php for this personality table and update other table with multiple category 我要做的就是为此性格表选中带有php的复选框选项,并更新具有多个类别的其他表

and my form: 和我的表格:

  <form>
    Name : <input name="user" type="text" id="user" value="<?php echo $line['user'];?>" />
    Personality : <?php $personality=mysql_query("select * from personality");
                  while($data=mysql_fetch_array($personality)){
                  $check = ($line['usr_char']==$data['category'])?"checked" : "";
                  <label for='$data[kategori]'>
                  <input type='checkbox' id='usr_char' checked value='$data[category]'>
                  <span class='custom checkbox $check'></span> $data[category]</label><br>}?>

How do I post the category of personality table into other table with multiple category? 如何将个性表的类别发布到具有多个类别的其他表中?

Here is example for the result : Table Name : people 这是结果的示例:表名称:people

  name | usr_char
  abby | cute,rich,funny
  andy | famous,funny

Create another table called people_personality , with the following: 使用以下people_personality创建另一个名为people_personality表:

  • id <-- your primary key id <-您的主键
  • person_id <-- your primary key from your "People" table person_id <-您的“人”表中的主键
  • personality_id <-- your primary key from your "Personality" table personality_id <-您的“ Personality”表中的主键

Suppose you have in your People table: 假设您的People表中有:

id - name
1  - abby
2  - andy

Now you want to assign abby cute , rich , and funny : 现在,您要为abby分配cuterichfunny

INSERT INTO people_personality VALUES ('',1,1),('',1,2),('',1,3)

For one you are missing a name on the input element. 对于一个,您在输入元素上缺少名称。

<input type='checkbox' id='usr_char' name='attribute[]' checked value='$data[category]'>

After that on the to get the value to update your people table with you would just use 之后,要获取值以更新您的人员表,只需使用

$attributes = implode(",", $_POST["attribute"]);

My suggestion is don't try to store multiple comma separated values in an SQL field . 我的建议是不要尝试在SQL字段中存储多个逗号分隔的值 It has many disadvantages including, 它有很多缺点,包括:

  • Retrieving the individual values is a tedious process 检索个人价值是一个乏味的过程
  • It is not readable 不可读
  • Becomes difficult to maintain 变得难以维护

So, instead use a many-to-many link table. 因此,改为使用多对多链接表。 For example, you can have tables this way: 例如,您可以通过以下方式创建表:

Table 1: 表格1:

Id | Category

 1 | cute
 2 | rich
 3 | stupid
 4 | funny
 5 | famous

Table 2: 表2:

Name | Usr_char

abby | cute
abby | rich
abby | funny
andy | famous
andy | funny

Having this way makes it easier for example, to add characters later and remove them and even to select them. 有了这种方式,例如,以后添加字符,删除字符甚至选择字符就变得更加容易。

To retrieve: 要检索:

SELECT * FROM table_name WHERE Name = 'abby';

To add: 加上:

INSERT INTO table_name (Name, Usr_char) VALUES ('abby', 'cute');

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

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