MySQL Query form data - php

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". 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. Column 1 title "Token", Column 2 title "Name" then list the values below each
--------------------------
| 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? 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.
Simply fetch the column value and use php function
$result = json_decode(data);
Now $result holds the data in array format.
Use for each to iterate the array and fetch each value.

First, let's take the JSON string that's in the Form column and turn it into an array with json_decode(). Assuming you've already retrieved the value from the Form column and assigned it to the variable $form:
$form = json_decode($form,true);
Next, we'll retrieve the Token value and the Name value:
$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.

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
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');

Related

How to retrieve data based on json column matching with special data

delivery_charges table has a json type column named locations. this column has the following data
[{"district_id":"1"},{"district_id":"2"},{"district_id":"3"}]
But $deliveryCharges is always an empty array when I run this code:
$deliveryCharges = DB::table('delivery_charges')
->whereJsonContains('locations->district_id',post('district_id'))
->get();
dump(post('district_id')); // outputs 1
dump($deliveryCharges); // outputs []
I can see value of post('district_id') is 1. And when I comment whereJsonContains() lines it return the data. So it seems to me there is a problem in my where clause.
If your column is named location, in the where clause you are named locations. Just use the correct column name:
$query->whereJsonContains('location->district_id',post('district_id'));
Try this
$deliveryCharges = DB::table('delivery_charges')
->where('locations','LIKE','%"district_id":"'.post('district_id') .'"%')
->get();

can we store multiple data on single id in database?

I have this following.
id name
1 Dhiraj Dhanaji Desai.
2 ram patil
3 aman mehta
I want this.
id name
1 Dhiraj Dhanaji Desai.
ram patil
aman mehta
You can store JSON value in a field for having "multiple" data in a single field.
for example, you can store this value:
{"id":1,"name":"Dhiraj Dhanaji Desai"}
and get it back parsing JSON. (for example php have a function json_decode)
references:
https://www.php.net/manual/en/function.json-decode.php
https://www.json.org

MYSQL Select IN query using text

Any quick assistance will be highly appreciated. I've table row in mysql named as mob_categories, now data is being is saved as '|' separated for instance (cat 1|cat 2|cat 3 and so on. Now i need to get the value from another column in the same table if the input value matches.
for instance if the value is cat 1 i need to select the value from another column is named as deveice_token wherever it matches with 'cat 1'
I tried this code but its not working somehow
SELECT * from table_name where find_in_set('cat 1',mob_categories) <> 0
so i modified a bit with the following code but it works with only numeric value if the value exits in this format in this format (i.e) 1,2,3
$userNotification = $wpdb->get_results("SELECT * from table_name where 1 IN (mob_categories); ");
What's i'm missing specifically?
Since data in mob_categories are separated by | rather than by comma (,) so you need to make it compatible for FIND_IN_SET first.
So replace all the | by comma (,) first.
SELECT * from table_name where find_in_set('cat 1', REPLACE(mob_categories,'|',',')) > 0

mysql_query inject - replace returned field

im trying to learn and understand mysql inject, i have created demo case.
SELECT ret_variable FROM data WHERE name = '".$name."' AND age = ".$age;
then if(ret_variable == 2){something} but query originally returns 1 and i need to force it to output 2
How to modify $age variable to set custom output field for ret_variable(only in response) ?
I have tried few ways with OR but didn't wroked.
I see no practical application other than learning. I assume since you know the code , you have permission to test this out. So let's give it a go!
You can only return a 2 for the ret_variable when there is a row in the database with a value of 2 as the ret_variable and you know the name value of that row. You can for instance enter that name and the following to bypass the correct value for the age.
age AND ret_value = 2
That would create the following query:
SELECT ret_variable FROM data WHERE name = 'John' AND age = age AND ret_value = 2;
The principle of mysql injection is this sort of manipulation of the query. But you can not force a value which is returned unless there is a row in the database with this value for ret_variable and you can somehow select this row.
When you don't know the name (or there is no record of your known name with a ret_variable of 2) it is not possible.
Since the AND operator has precedence over the OR operator you cannot manipulate the query to give a 2 as ret_variable. This is because the name = '?' part will always fail.

MySQL query to return json name value

I'm working with a table in which information is stored in a table in JSON format. The JSON value field looks like:
select * from k2_extra_fields where id = 2 and published = 1;
id | value
2,[{"name":"Apples","value":1,"target":null,"alias":"","required":0,"showNull":1},{"name":"Pears","value":2,"target":null,"alias":"","required":0,"showNull":1},{"name":"Mangos","value":3,"target":null,"alias":"","required":0,"showNull":1},{"name":"Guava","value":4,"target":null,"alias":"Fruit","required":0,"showNull":1},{"name":"Pineapple","value":5,"target":null,"alias":"Fruit","required":0,"showNull":1}]
Or values in a simple line by line view (minus the ID):
[
{"name":"Apples","value":1,"target":null,"alias":"","required":0,"showNull":1},
{"name":"Pears","value":2,"target":null,"alias":"","required":0,"showNull":1},
{"name":"Mangos","value":3,"target":null,"alias":"","required":0,"showNull":1},
{"name":"Guava","value":4,"target":null,"alias":"Fruit","required":0,"showNull":1},
{"name":"Pineapple","value":5,"target":null,"alias":"Fruit","required":0,"showNull":1}
]
The query that leads me here returns the value of 3. 3 = Mangos. How do I take the '3' value and match it up with the stored names/values so that I end up with the output, Mangos?
It should be possible with build in mysql functionality, but very hard and 'not clever' idea to do. If you really need to compute this problem within mysql, you would need to actually add new funtionality to your mysql. Look up on UDF plugins: http://dev.mysql.com/doc/refman/5.1/en/udf-compiling.html

Categories