简体   繁体   English

MySQL查询表单数据

[英]MySQL Query form data

I have a php form that when submitted sends form values into a MySQL Database named "Hotel" in a table named "Reservations" that has one column titled "Form".我有一个 php 表单,当提交表单值时,它会将表单值发送到名为“预订”的表中名为“酒店”的 MySQL 数据库中,该表中有一个名为“表单”的列。 In the "Form" column, each form field is enclosed within {} and fields are separated by commas.在“表单”列中,每个表单字段都包含在 {} 内,字段之间用逗号分隔。 Here is what the data looks like in the "Form" column:以下是“表单”列中的数据:

[{"id":"1","translation":"Token","value":"123456789"},
 {"id":"2","translation":"Name","value":"John Smith"}]

Desired MySQL Query Result: I want to grab each "translation" and "value" in my query and have them put into separate columns.所需的 MySQL 查询结果:我想获取查询中的每个“翻译”和“值”,并将它们放入单独的列中。 Column 1 title "Token", Column 2 title "Name" then list the values below each第 1 列标题“令牌”,第 2 列标题“名称”,然后列出每个值下面的值

--------------------------
| Token     | Name       |
--------------------------
| 123456789 | John Smith |
--------------------------

I have never run into this kind of data in a column before so I am unsure how to create the query.我以前从未在列中遇到过这种数据,所以我不确定如何创建查询。 I'm thinking substring perhaps?我在想 substring 也许? Any help or guidance is greatly appreciated.非常感谢任何帮助或指导。 Please let me know if more information is need from me to help process the request.如果需要我提供更多信息来帮助处理请求,请告诉我。

The form column has data format in json.表单列的数据格式为 json。 Simply fetch the column value and use php function $result = json_decode(data);只需获取列值并使用 php 函数 $result = json_decode(data); Now $result holds the data in array format.现在 $result 以数组格式保存数据。 Use for each to iterate the array and fetch each value.使用 for each 迭代数组并获取每个值。

First, let's take the JSON string that's in the Form column and turn it into an array with json_decode() .首先,让我们获取Form列中的 JSON 字符串,并使用json_decode()将其转换为数组。 Assuming you've already retrieved the value from the Form column and assigned it to the variable $form :假设您已经从Form列中检索到值并将其分配给变量$form

$form = json_decode($form,true);

Next, we'll retrieve the Token value and the Name value:接下来,我们将检索Token值和Name值:

$token = $form[0]["value"];
$name = $form[1]["value"];

Note: This assumes that 'token' always occurs first in the 'form' string, and that 'name' always occurs second.注意:这里假设 'token' 总是首先出现在 'form' 字符串中,而 'name' 总是第二次出现。

You can it do like this:你可以这样做:

Samples to get the Values:获取值的示例:

SELECT REGEXP_REPLACE('[{"id":"1","translation":"Token","value":"123456789"},{"id":"2","translation":"Name","value":"John Smith"}]',
                      '^.*"translation":"Token","value\":"([0-9]+)".*$','\\1') AS Token;

RESULT: 123456789结果:123456789

SELECT REGEXP_REPLACE('[{"id":"1","translation":"Token","value":"123456789"},{"id":"2","translation":"Name","value":"John Smith"}]',
                      '^.*"translation":"Name","value\":"(.*)".*$','\\1') AS Name;

RESULT: John Smith结果:约翰·史密斯

To Update the Table:更新表格:

update TABLENAME set
  TOKENFIELD = REGEXP_REPLACE(JSONFIELD,{"id":"2","translation":"Name","value":"John Smith"}]',
                          '^.*"translation":"Token","value\":"([0-9]+)".*$','\\1'),
  NAMEFIELD = SELECT REGEXP_REPLACE(JSONFIELD,{"id":"2","translation":"Name","value":"John Smith"}]',
                          '^.*"translation":"Name","value\":"(.*)".*$','\\1');

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

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