Writing specific values to a database X times? - php

To begin with, i should clarify the title, so you really understand what I'm trying to do.
I'm working on a shopping-cart-ish script, but it's a little different because all products (almost always) will be purchased each time. Therefore i believe it's best to list all of the items up with a table, and then let the customer choose the amount of each product they want.
It'd look something like this:
-- Name ------------------------- Amount --
-- A random product ------------- 15 ------
-- Another random product ------- 2 -------
-- And a third one --- ---------- 19 ------
-- ------------------------------ ---------
-- ------------------------------ SUBMIT --
The problem is, I'm not sure how to write it into the database. Or more specific, i don't know how to submit the data in a way that i get all of the chosen products, as well as the amount of each one into an array. I might add more products to the database later, so i can't hardcode it in.
I can't post two values in one input, which is too bad. If i could do that i could post the ID and the amount as two different values.
Any ideas how to do this?

When you create your input fields, use an array for the name, with the product id as the key.
For example (where $product_id is the ID of the product that the amount corresponds to):
<input type="text" name="amounts[<?=$product_id?>]">
Then in PHP, after the form is posted, $_POST['amounts'] will be an array, where the keys are the product IDs, and the values are the actual amounts. You do not need to do anything special for this to happen; by default, PHP will recognize that GET/POST data that contains []s should be made into an array.
Then you can just iterate over this array to update the database:
foreach ($_POST['amounts'] as $product_id => $amount)
{
if (!empty($amount)) // was a value given for this amount?
{
// build your insert query here
}
}

concatenate the productID and the qty with a pipe "|" so it would be
productId=14
Qty= 3
inserted value= 14|3
then after your db fetch, you can explode the data by the "|" and have part1=14 and part2=3

Related

How to store variable length arrays in a database

I'm looking for a way to save data based off a set of rows in another table, but I don't know how to set up the field. Think something similar to using the results of a mysql group_concat as the field. The data is based off the unique combination of rows, rather than one row or field.
What I need to be able to do is:
Store the array itself in the database
Store associated data about the array
Retreive the array
The ability to lookup data about the array using the data in the array
Some options I've thought about:
Saving as an ordered set concatenated into a string.
Saving the serialized array (serialized using php's serialize function).
Saving the set as a hashed string using a reversible hash.
None of these options seem correct so I came here hoping someone has a better answer.
Background:
Supposed I have the following tables:
users {id, other unimportant fields}
products {id, other unimportant fields}
shipments {id, user_id, product_id, date, other unimportant fields}
I want to create a new table called assigned_products where the assigned product is based off of the unique combination of products they've received in the past. So assigned_products should look like:
assigned_products {set_of_products_received (array), product_id (data about the array)}
I don't know of a good way to store set_of_products_received in a database.
Example use:
Suppose I have 100 users who got product A, 100 users who got product B, and 100 users who got products A and B. Suppose then I wanted to give product B to everyone who got product A, product A to everyone who got product B, and product C to everyone who got product A and B. The assigned products table should look like:
+--------------------------+------------+
| set_of_products_received | product_id |
+--------------------------+------------+
| A | B |
| B | A |
| A, B | C |
+--------------------------+------------+
I'm just looking for a better way of storing set_of_products_received
Reading this over I realize it's a bit hard to understand, but I don't really know the appropriate terms to describe this issue (probably why I'm having trouble finding solutions). I'll be happy to clarify if anyone has any questions.

Update Json string that stored on DB

I have DB table that one of his Column contain a JSON string like this:
{"freet1":"\u05d2'\u05dc\u05d0\u05e8\u05d8\u05d4 \u05de\u05d5\u05d3\u05d9\u05e2\u05d9\u05df - \u05de\u05d5\u05d3\u05d9\u05e2\u05d9\u05df - \u05ea\u05e4\u05e8\u05d9\u05d8 \u05de\u05e9\u05dc\u05d5\u05d7\u05d9\u05dd - Ice Cream | Tinder","freet2":"5% \u05d4\u05e0\u05d7\u05d4","freet3":"\u05dc\u05de\u05d6\u05de\u05d9\u05e0\u05d9\u05dd \u05d3\u05e8\u05da \u05d4\u05d0\u05ea\u05e8","freet6":"\u05d4\u05d4\u05d6\u05de\u05e0\u05d4 \u05e9\u05dc\u05d9","freet4":"\u05d0\u05d9\u05df \u05d0\u05e4\u05e9\u05e8\u05d5\u05ea \u05dc\u05e9\u05e2\u05e8\u05da \u05d0\u05ea \u05d4\u05d4\u05d6\u05de\u05e0\u05d4 \u05de\u05db\u05d9\u05d5\u05d5\u05df \u05e9\u05d4\u05de\u05d5\u05e6\u05e8\u05d9\u05dd \u05e9\u05e7\u05d9\u05dc\u05d9\u05dd. \u05de\u05d9\u05e0\u05d9\u05de\u05d5\u05dd \u05de\u05e9\u05dc\u05d5\u05d7 45 \u05e9\"\u05d7","0":{"menu":"\u05d2\u05dc\u05d9\u05d3\u05d5\u05ea \u05e9\u05de\u05e0\u05ea","currentmenufile":"142525634133.JPG","data":[{"product":"\u05e9\u05d5\u05e7\u05d5\u05dc\u05d3 \u05de\u05e0\u05d8\u05d4","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425056679\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425056679\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05e9\u05d5\u05e7\u05d5\u05dc\u05d3 \u05d1\u05dc\u05d2\u05d9","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425056687\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425056687\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05e9\u05d5\u05e7\u05d5\u05dc\u05d3 \u05dc\u05d1\u05df","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425056695\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425056695\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05e9\u05d5\u05e7\u05d5\u05dc\u05d3 \u05e7\u05d9\u05e0\u05d3\u05e8","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425056702\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425056702\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05e9\u05d5\u05e7\u05d5\u05dc\u05d3 \u05ea\u05e4\u05d5\u05d6","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425056727\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425056727\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05e8\u05d9\u05d1\u05ea \u05d7\u05dc\u05d1","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425056815\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425056815\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05dc\u05d5\u05d8\u05d5\u05e1","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425056823\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425056823\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05e7\u05e4\u05d5\u05e6'\u05d9\u05e0\u05d5","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425056830\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425056830\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05d5\u05d9\u05e1\u05e7\u05d9","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425056944\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425056944\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05d5\u05e0\u05d9\u05dc \u05e6\u05e8\u05e4\u05ea\u05d9","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425056953\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425056953\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05e2\u05d5\u05d2\u05d9\u05d5\u05ea \u05d0\u05d5\u05e8\u05d0\u05d5","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425056962\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425056962\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05de\u05e7\u05d5\u05e4\u05dc\u05ea","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425057129\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425057129\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05d5\u05e0\u05d9\u05dc \u05e7\u05dc\u05d9\u05e7","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425057136\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425057136\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05e4\u05e0\u05d9\u05e0\u05ea \u05d9\u05e2\u05e8","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425057143\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425057143\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05d8\u05d5\u05d5\u05d9\u05e7\u05e1","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425057472\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425057472\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05e2\u05d5\u05d2\u05ea \u05d2\u05d1\u05d9\u05e0\u05d4","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425057480\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425057480\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05ea\u05d5\u05ea \u05d1\u05e9\u05de\u05e0\u05ea","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425057487\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425057487\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05e4\u05e8\u05e8\u05d5 \u05e8\u05d5\u05e9\u05d4","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425058752\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425058752\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05e7\u05e8\u05de\u05dc \u05de\u05dc\u05d5\u05d7","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425058759\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425058759\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05d8\u05d5\u05e4\u05d9\u05e4\u05d9","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425058768\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425058768\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05d1\u05d0\u05d5\u05e0\u05d8\u05d9","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425060944\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425060944\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05e4\u05d9\u05e1\u05d8\u05d5\u05e7","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425060952\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425060952\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05d5\u05e4\u05dc \u05de\u05d9\u05d9\u05e4\u05dc","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425060960\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425060960\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05e0\u05d5\u05d8\u05dc\u05d4","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425063548\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425063548\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05dc\u05d5\u05d0\u05e7\u05e8","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425063559\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425063559\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05d7\u05dc\u05d1\u05d4","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425063567\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425063567\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05e7\u05e8\u05dd \u05e9\u05e0\u05d9\u05d8","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425256001\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425256001\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05d1\u05e0\u05e0\u05d4 \u05e4\u05e7\u05d0\u05df","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425410592\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425410592\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05e1\u05e0\u05d9\u05e7\u05e8\u05e1","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425256019\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425256019\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05d0\u05dc\u05e4\u05d7\u05d5\u05e8\u05e1","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425410136\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425410136\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05d8\u05d9\u05e8\u05de\u05d9\u05e1\u05d5","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425410144\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425410144\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05de\u05d2\u05e0\u05d5\u05dd","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425410156\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425410156\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05e8\u05e4\u05d0\u05dc\u05d5","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425410168\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425410168\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05d8\u05e2\u05de\u05d9","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425410185\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425410185\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05de\u05d9\u05dc\u05e7\u05d9","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425410193\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425410193\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05d9\u05d5\u05d2\u05d5\u05e8\u05d8 \u05d5\u05e4\u05d9\u05e8\u05d5\u05ea \u05d9\u05e2\u05e8","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425410202\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425410202\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"}],"menufile":"142525634133.JPG"},"1":{"menu":"\u05d2\u05dc\u05d9\u05d3\u05d5\u05ea \u05e1\u05d5\u05e8\u05d1\u05d4","currentmenufile":"142532108933.JPG","data":[{"product":"\u05dc\u05d9\u05de\u05d5\u05df","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425410247\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425410247\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05dc\u05d9\u05de\u05d5\u05e0\u05e2\u05e0\u05e2","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425410247\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425410247\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05ea\u05d5\u05ea \u05e9\u05d3\u05d4","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425410255\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425410255\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05e4\u05e1\u05d9\u05e4\u05dc\u05d5\u05e8\u05d4","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425410270\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425410270\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05de\u05e0\u05d2\u05d5","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425410270\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425410270\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05ea\u05e4\u05d5\u05d7 \u05d9\u05e8\u05d5\u05e7","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425410270\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425410270\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05e4\u05d9\u05e8\u05d5\u05ea \u05d9\u05e2\u05e8","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425410278\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425410278\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05e9\u05d5\u05e7\u05d5\u05dc\u05d3 \u05de\u05e8\u05d9\u05e8","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425410284\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425410284\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05e7\u05d5\u05e7\u05d5\u05e1","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425410290\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425410290\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05d3\u05d9\u05d0\u05d8 \u05e4\u05d9\u05e8\u05d5\u05ea","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"u","pnew":"u","psale":"u","pactive":"y","currentfile":"1425410297\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425410297\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"}],"menufile":"142532108933.JPG"},"2":{"menu":"\u05d2\u05dc\u05d9\u05d3\u05d5\u05ea \u05e1\u05d5\u05d9\u05d4","currentmenufile":"142532142833.JPG","data":[{"product":"\u05d1\u05d5\u05d8\u05e0\u05d9\u05dd","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425410304\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425410304\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05d7\u05dc\u05d1\u05d4","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425410310\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425410310\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05e4\u05d9\u05e1\u05d8\u05d5\u05e7","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425410321\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425410321\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05dc\u05d5\u05d8\u05d5\u05e1","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425410316\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425410316\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05d0\u05d2\u05d5\u05d6\u05d9 \u05dc\u05d5\u05d6","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425410327\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425410327\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"}],"menufile":"142532142833.JPG"},"3":{"menu":"\u05e7\u05d9\u05e0\u05d5\u05d7\u05d9\u05dd","currentmenufile":"142564673833.JPG","data":[{"product":"\u05e7\u05e8\u05e4 \u05e6\u05e8\u05e4\u05ea\u05d9","desc":"","price":"18","pgraysmall":"\u05dc\u05d9\u05d7'","pgreen":"","unit":"u","pnew":"u","psale":"u","pactive":"y","currentfile":"1425646773\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425646773\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05e7\u05e8\u05e4 + \u05d2\u05dc\u05d9\u05d3\u05d4 + \u05e7\u05e6\u05e4\u05ea","desc":"","price":"30","pgraysmall":"\u05dc\u05d9\u05d7'","pgreen":"","unit":"u","pnew":"u","psale":"u","pactive":"y","currentfile":"1425646783\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425646783\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05d5\u05d5\u05e4\u05dc \u05d1\u05dc\u05d2\u05d9","desc":"+ 2 \u05db\u05d3\u05d5\u05e8\u05d9\u05dd + \u05e7\u05e6\u05e4\u05ea \u05d5\u05e1\u05d9\u05e8\u05d5\u05e3","price":"49","pgraysmall":"\u05dc\u05d9\u05d7'","pgreen":"","unit":"u","pnew":"u","psale":"u","pactive":"y","currentfile":"1425646790\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425646790\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05e1\u05d5\u05e4\u05dc\u05d4 \u05e9\u05d5\u05e7\u05d5\u05dc\u05d3 \u05d7\u05dd","desc":"+ \u05db\u05d3\u05d5\u05e8 \u05d2\u05dc\u05d9\u05d3\u05d4 + \u05e7\u05e6\u05e4\u05ea \u05d5\u05e1\u05d9\u05e8\u05d5\u05e3","price":"38","pgraysmall":"\u05dc\u05d9\u05d7'","pgreen":"","unit":"u","pnew":"u","psale":"u","pactive":"y","currentfile":"1425796952\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425796952\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"}],"menufile":"142564673833.JPG"}}
Is it possible to update only 1 "product" in this way? Because if I have more then 1,000 products the update is very slow (update all the string)
Better way then get the string by SQL Select the decode,change spesific values and then update the full string?
Thanks!
To update part of a JSON string stored in MySQL, you'll indeed need to retrieve, decode to an array/object, edit, encode as JSON, and store. It's time-consuming.
I'd recommend a change to your table schema. Not sure how it's set up right now, but instead of having one table with all of your info, you could have one table just for Products, another for Orders, another for Customers, etc.
So in your Products table, you'd have one row per product:
ID | NAME | DESCRIPTION | PRICE
00123| jacket | gray wool jacket | $4.99
Then in another table when you need to reference this product, you simply need to use the ID value. You can update the table rows for products much easier than editing their JSON.
Regarding your first question, you cannot update only one product. There should be a row for each product, sounds like a terrible database design. It could make some sense if it was an static page but if you want to update or delete products it's gonna be really costly.
EDIT: Obviously when I said "you cannot update only one product" I meant to say that you must decode JSON string, modify the resulting array and encode it again to update a single product". It would have been more correct to say "you cannot update only one product without having to update the whole JSON array".

Have an array in a SQL field. How to display it systematically?

I have a field in a data feed coming in with some values separated by commas. For one record, the values are:
A06,C05,C06,C15,C18,C19,C21,C22,E05,E22,G11,J02,J07,L04,L07,M01,M05,N03,N07,N10,N11,N12,N18,N19,N20,N24,O02,O03,O04,O06,O09,O14,O15,O16,O20,O21,O31,Q01,Q04,Q08,R07,S08,T08,T12,T23,T32,U01,U03,U04,U06,U13,W09,W11,W16,W19,W30,W45,X02,X03,X12,Z07
I have a separate table with some descriptions as to what each code means. When I query the main table and get this field name as a value, I can use explode to get it into an array and use a foreach loop to output each value.
The problem is, I want to display the description stored in another table. What's the proper way of iterating through this to display these values in a list?
As an example, C21 means "Gated Community."
You can use FIND_IN_SET() function for that.
Example you have record like this
Orders Table
------------------------------------
OrderID | attachedCompanyIDs
------------------------------------
1 1,2,3 -- comma separated values
2 2,4
and
Company Table
--------------------------------------
CompanyID | name
--------------------------------------
1 Company 1
2 Another Company
3 StackOverflow
4 Nothing
Using the function
SELECT name
FROM orders, company
WHERE orderID = 1 AND FIND_IN_SET(companyID, attachedCompanyIDs)
will result
name
---------------
Company 1
Another Company
StackOverflow
As you have tagged codeigniter you could use the built in Active Record's method $this->db->where_in(); to get the description. For example consider the code below
$codes = array('A06', 'C05', 'C06');
$this->db->where_in('description', $codes);
// Produces: WHERE codes IN ('A06', 'C05', 'C06')
For more information about Active Records of Codeigniter refer Active Record Class
For more information on how mysql WHERE IN works refer Tutorial
Just for follow the books, the best way of doing this in SQL language is to use the relationship.
For understand this I recommend you read this simple paper http://net.tutsplus.com/tutorials/databases/sql-for-beginners-part-3-database-relationships/
and maybe this http://www.informit.com/articles/article.aspx?p=30875&seqNum=5 for SELECT the data, or search for yourself on the web, and I recommend you to try yourself the examples. SQL Relationship Is good and necessary for security and many others reasons.

MySQL search in field (or other solutions)

I have a table with products that fall under specific categories, but the products within each category can contain multiple meta data tracking field
Table: products
id name category metadata
1 something 1 blue,red,purple
2 something else 2 left,right,middle
I have been trying to contemplate the best method to have a single product table but can't seem to squeeze the metadata in conveniently. for now I have created a table with all the metadata and fields for tracking the related category (the sequence is so i can order them withing a dropdown etc..)
Updated table: products
id name category metadata
1 something 1 1,2,3
2 something else 2 4,5,6
Table: metadata
id category sequence option
1 1 1 blue
2 1 2 red
3 1 3 purple
4 2 1 left
5 2 2 right
6 2 3 middle
If this format makes sense .. I am trying to generate a query that will search for values in my product table and grab each and all of the related meta values. The issue I am having is trying to find a unique value in the products field. if I do a MySQL search for LIKE(%1%) I will get matches for 1, 11, 21, 31 etc ... I thought of adding a leading and trailing comma to the field by default and then search for ",1," which would be unique .. but there has to be a better way ...
Any recommendations (regarding format or query)?
It's not an ideal design to have comma-separated values within a single database field. Aside from the problem you mentioned (difficult to search), your queries will be less efficient, as the DB won't be able to use indices for the lookup.
I'd recommend making a separate table products_metadata with a many-to-one relationship to the products table. Have the *metadata_id*, and the *product_id*, which is a foreign key linking back to the products table. That will make your job much easier.
You want to add another table, which links products to their metadata. It will have two columns: productid and metadataid which refer to the relevant entries in the products and metadata tables respectively. Then you no longer keep metadata in the products table, but JOIN them together as required.

Generic structuration of a web form and a MySQL's table for saving a array of undefined length

In a web form i will ask the user for their job experiences, this data will have no fixed lenght. I need to let the user insert all the items he needs, every item will content 3 fields; job title, description and year.
My firts problem is, how can i ask in the html form for the items? i mean, whats the best way to ask items with no fixed lenght using html/php (and maybe ajax)? I saw some sites that have a button (add one) when you hit it a new item slot is showed, but i have no idea of how to implement this, an example will be sufficient.
The second part is, how can i managed the data flow in post or get?, until now, i only use fixed fields, so i always know in my php script how many post or get vars i will get. How can i use multiple POST vars without knowing the amount of them?
And the last one (and the more important), how will be the best structure for my table in MySQL? If i get multiple items for a fixed table where i will have all my users, how can i resolve the multiple items issue? For example, if my table is:
User | password | job_experiences
admin | root | (this is just a cell, how can i save multiple items here?)
jonh | 1234 | (this is just a cell, how can i save multiple items here?)
Thanks for any help!!!!
Those are 3 questions, and it's best to post 3 questions, instead of discussing all of them. I will post the basics, and if you have specific questions, ask.
First, use button to add, and a JavaScript to clone an existing row (which can have more then one input field). For fieldnames use something like company_name[] - the [] is the important part, at this will send the field as an array. If you are editting profile, you can use company_name[$id] to preserve the mapping.
Second, in PHP you will receive this as $_POST['company_name'] which will be numeric array with all the company names. Or if you specify $id - with the corresponding keys. So, you have to loop trough all company_names, if there are other fields - you retrieve them the same way, using the current key. Example:
for (i =0; i<$_POST['comany_name'].length;i++) {
$company = $_POST['comany_name'][$i];
$start_year = $_POST['from'][$i];
...
}
Next, you need 1 table for the users (username, password), and another for job experiences (userid, company, description, from, to). This is called 1:M relation

Categories