如何在数据库中保存复选框数组?

时间:2022-04-08 13:27:13

I have this form:

我有这种形式:

    <tr>
   <td><input type="hidden" name="ledlamps" value="LED lamps">LED lamps:</td>
   <td><input class="field checkbox" type="checkbox" name="box[]" value="3mm"/><label class="choice">3mm</label></td>
   <td><input class="field checkbox" type="checkbox" name="box[]" value="5mm"/><label class="choice">5mm</label></td>
   <td><input class="field checkbox" type="checkbox" name="box[]" value="8mm"/><label class="choice">8mm</label></td>
   <td><input class="field checkbox" type="checkbox" name="box[]" value="10mm"/><label class="choice">10mm</label></td>
   <td><input class="field checkbox" type="checkbox" name="box[]" value="Ovals"/><label class="choice">Ovals</label></td>
   <td><input class="field checkbox" type="checkbox" name="box[]" value="Assembly LEDs"/><label class="choice">Assembly LEDs</label></td>
  </tr>

and this php code:

这个php代码:

    $box=$_POST['box'];
$ledlamps = $_POST['ledlamps'];

if ($box != 0) {
echo "$ledlamps: ";
while (list ($key,$val) = @each ($box)) {
$val1 = "$val, ";
echo "$val1";
}

}

If I output with echo it displays in the way I want it:

如果我输出echo它会以我想要的方式显示:

Led lamps: 3mm, 5mm, 8mm (if i tick the respective checkboxes)

Led灯:3mm, 5mm, 8mm(如果我选择相应的复选框)

But I want to store this in a mysql table field. How do I do that?

但我想把它存储在mysql表字段中。我该怎么做呢?

Thanks for your help!

谢谢你的帮助!

3 个解决方案

#1


2  

You can use implode() to join the $box array in to one string as follows:

可以使用内爆()将$box数组加入到以下字符串中:

$box=$_POST['box'];
$ledlamps = $_POST['ledlamps'];

$str = $ledlamps . ": " . implode(", ", $box);

Then $str should contain "Led lamps: 3mm, 5mm, 8mm" (depending on the fields you've checked).

然后$str应该包含“Led灯:3mm, 5mm, 8mm”(取决于您检查的字段)。

This field can then be inserted in to whatever mysql column you need (assuming here that your query is properly escaped and input validated before attempting a db query..).

然后可以将该字段插入到需要的任何mysql列中(假设您的查询已正确转义并在尝试db查询之前进行输入验证)。

This way of storing the data will likely make it quite tricky to query the data again should you wish to pull it out again, so while it should suffice for simple printing to screen of something like a product history, you may be better investigating whether your db table structure could be made more accommodating. So if we assumed this was for a stock control system, something like:

这种方式存储的数据可能会让它很棘手的再次查询的数据应该要把它拿出来,因此,它应该满足简单的打印屏幕类似产品的历史,你可能会更好的调查你的数据库表结构是否可以更加适应。如果我们假设这是一个股票控制系统,比如:

category table:
- category_id
- category_name      (LED lamps..)

stock table:
- item_id
- category_id        (id for LED lamps..)
- item_description   (3mm lamp, etc)

stock_selection table:
- transaction_id
- user_id
- date

stock_transaction table:
- transaction_id
- item_id
- change             (number of items added/removed)

So once you receive back the checkboxes from your form, you create a new stock_selection record for the logged-in user. You then use the id from this new record and make 1 entry per checkbox in the stock_transaction table.

因此,一旦从表单接收到复选框,就为登录的用户创建一个新的stock_selection记录。然后使用这个新记录中的id,并在stock_transaction表中每个复选框中创建一个条目。

This would allow you in future to query your database to keep track of things like how many items have been taken out (or re-added, etc), who took them, etc, and may ultimately be a more future-proof approach, depending on your overall architecture.

这将允许您在将来查询数据库,以跟踪已取出(或重新添加)的项目数量、使用这些项目的人等等,最终可能是一种更具有前瞻性的方法,这取决于您的总体架构。

#2


2  

Either store them in the database serialized as suggested by @Sarfraz, or comma separated, or in a separate table, joined with the primary table.

要么按照@Sarfraz的建议将它们存储在序列化的数据库中,要么将它们分隔开,要么将它们存储在与主表相连接的单独的表中。

#3


1  

You could either:

你可以:

$boxes = implode(",", $_POST['boxes']);
$id = intval($_GET['id']);
$sql = sprintf("UPDATE table SET box=%s WHERE id=%d", $boxes, $id);
$res = mysql_query($sql);

This would store box values in a comma-separated array, which you could then explode() when retrieving from the database.

这将在逗号分隔的数组中存储框值,当从数据库中检索时,您就可以将其爆炸()。

Alternatively, you could set up a separate table if you want to go the proper route. That way you could have a many-to-one relationship, where one object (let's say car) can have many lamps.

或者,如果您想要走正确的路线,您可以设置一个单独的表。这样就可以有一个多对一的关系,其中一个对象(比如car)可以有多个灯。

Table: cars
  - ID
  - Name

Table: cars_lamps
  - Car_ID
  - Lamp_ID

Table: Lamps
  - ID
  - Name

I hope that makes sense.

我希望这说得通。

#1


2  

You can use implode() to join the $box array in to one string as follows:

可以使用内爆()将$box数组加入到以下字符串中:

$box=$_POST['box'];
$ledlamps = $_POST['ledlamps'];

$str = $ledlamps . ": " . implode(", ", $box);

Then $str should contain "Led lamps: 3mm, 5mm, 8mm" (depending on the fields you've checked).

然后$str应该包含“Led灯:3mm, 5mm, 8mm”(取决于您检查的字段)。

This field can then be inserted in to whatever mysql column you need (assuming here that your query is properly escaped and input validated before attempting a db query..).

然后可以将该字段插入到需要的任何mysql列中(假设您的查询已正确转义并在尝试db查询之前进行输入验证)。

This way of storing the data will likely make it quite tricky to query the data again should you wish to pull it out again, so while it should suffice for simple printing to screen of something like a product history, you may be better investigating whether your db table structure could be made more accommodating. So if we assumed this was for a stock control system, something like:

这种方式存储的数据可能会让它很棘手的再次查询的数据应该要把它拿出来,因此,它应该满足简单的打印屏幕类似产品的历史,你可能会更好的调查你的数据库表结构是否可以更加适应。如果我们假设这是一个股票控制系统,比如:

category table:
- category_id
- category_name      (LED lamps..)

stock table:
- item_id
- category_id        (id for LED lamps..)
- item_description   (3mm lamp, etc)

stock_selection table:
- transaction_id
- user_id
- date

stock_transaction table:
- transaction_id
- item_id
- change             (number of items added/removed)

So once you receive back the checkboxes from your form, you create a new stock_selection record for the logged-in user. You then use the id from this new record and make 1 entry per checkbox in the stock_transaction table.

因此,一旦从表单接收到复选框,就为登录的用户创建一个新的stock_selection记录。然后使用这个新记录中的id,并在stock_transaction表中每个复选框中创建一个条目。

This would allow you in future to query your database to keep track of things like how many items have been taken out (or re-added, etc), who took them, etc, and may ultimately be a more future-proof approach, depending on your overall architecture.

这将允许您在将来查询数据库,以跟踪已取出(或重新添加)的项目数量、使用这些项目的人等等,最终可能是一种更具有前瞻性的方法,这取决于您的总体架构。

#2


2  

Either store them in the database serialized as suggested by @Sarfraz, or comma separated, or in a separate table, joined with the primary table.

要么按照@Sarfraz的建议将它们存储在序列化的数据库中,要么将它们分隔开,要么将它们存储在与主表相连接的单独的表中。

#3


1  

You could either:

你可以:

$boxes = implode(",", $_POST['boxes']);
$id = intval($_GET['id']);
$sql = sprintf("UPDATE table SET box=%s WHERE id=%d", $boxes, $id);
$res = mysql_query($sql);

This would store box values in a comma-separated array, which you could then explode() when retrieving from the database.

这将在逗号分隔的数组中存储框值,当从数据库中检索时,您就可以将其爆炸()。

Alternatively, you could set up a separate table if you want to go the proper route. That way you could have a many-to-one relationship, where one object (let's say car) can have many lamps.

或者,如果您想要走正确的路线,您可以设置一个单独的表。这样就可以有一个多对一的关系,其中一个对象(比如car)可以有多个灯。

Table: cars
  - ID
  - Name

Table: cars_lamps
  - Car_ID
  - Lamp_ID

Table: Lamps
  - ID
  - Name

I hope that makes sense.

我希望这说得通。