How add data to existing records in table - php

I have a table like
user_id | settings
settings stored in json format like
{"user_lang":"en","shop_name":"PayBot"}
I need to add data like "user_id: 1" to each settings record.
In result it have to like this
{"user_lang":"en","shop_name":"PayBot","user_id: 1"}
How I understand I gave to open each record, encode to array, push to array "user_id: 1" and save.
But how to do this in php? :)
Can anybody to provide me a short example?
Thanks!:)

You can do it by different ways. One way is with JSON_SET.
UPDATE your_table_name
set settings = JSON_SET(settings, "$.user_id", 1)
WHERE user_id = 1;
You can remove the condition and add the column name user_id in place of static value 1 to apply all rows for settings column with their corresponding user_id value
REF.: https://database.guide/json_set-insert-or-update-values-in-a-json-document-in-mysql/

Related

Updated Individual Rows in PHP/MYSQL

I am retriving data from a SQL DB which I am stroring in a form. I have a update page however, I dont know how I can update table according to what the user wishes to change and hence update the database.
This is what I mean. Say after running the query I have the output :
Product | Descption | Quantity | Price | StartDate | End Date
chcolates xyz 2.0 4.99 2013-03-11 2013-03-20
sweets yum! 1.0 1.99 2013-03-15 2013-03-27
so this is being outputted as a table in php by queuing the db.(i.e. this seller is selling 2 products mentioned).
Now say the user wanted to update the first row and change the quantity for example. Then how can I do this? I know I can use the SQL QUERY UPDATE TABLE sellers WHERE ??
Should I output a table or a form? The user is able to update any row they wish and any field also. (i.e. can update the product they wish to sell, descp, quantity etc.)
How can I go about doing something like this? Any ideas please?
====EDIT======
Thanks for everyones suggestions. I have a formatting error. I am inside my php tags and after running the query as such:
while($rows = mysql_fetch_assoc($querySellers)){
echo 'Product:'.'<br />'.'<input type="text" value = $rows[\'ProductName\']>';
This does not seem to be printing the VALUE of the query from the DB and instead prints
$rows['ProductName']
inside the text box this is a formatting error right? What can I do to solve this? And also initially the productName is a drop down combo box... Is there a way I can do it so that when I run the query the output is shown as the SELECTED combo box option?
Thanks once again.
The best thing you should do is implement an ID column (auto increment maybe?) or some unique identifier (Primary Key) to the items in the table. This way, you can easily edit one item by doing
UPDATE table SET value = 'value' WHERE id = $id
In your case, you do not have any unique identifier so you would have to build a large WHERE statement to include everything ie.
WHERE Product = 'chocolates' AND Description = 'xyz' AND .. AND..
and this may not be entirely unique.
EDIT: (in reference to edit)
The problem is how PHP handles single quotes vs. double quotes. PHP will NOT evaluate variables in single quotes but will in double quotes.
$myvar = "value";
echo '$myvar'; // output: $myvar
echo "$myvar"; // output: value
So you need to change your construction
echo "Product: <br /> <input type=\"text\" value = \"{$rows['ProductName']}\">";
Notice the change of single quotes to double and the escaping of the double quotes.
As for your question on the selected option. You need a way to determine which is selected and add an IF statement with the code to add Selected attribute to the option.
You need to have an id with primary key that auto increaments, not null.
THen
UPDATE `your_db_table`.`column_table` SET `Product`
= 'Sweet Stuff' WHERE 'Product`.`prod_id` = 1 LIMIT 1;
Then the rest of the stuff is is clearly explained by #UnholyRanger

Mysql Insert Order Number By VARCHAR Field

I have a question about inserting row order number by spesific order type.
Products table has OrderNumber field. I want to programaticly add new line to appropriate OrderNumber by its name. If the reference column would be integer it has to be easy like that
update products set OrderNumber=OrderNumber+1 where Price>555
Is there similar way for varchar field like
update products set OrderNumber=OrderNumber+1 where Name>'bla%'
Thank you
You can use STRCMP('text', 'text2')
update products
set OrderNumber=OrderNumber+1
where STRCMP(Name, 'bla') = 1;
I missunderstood your point. Can you try something like this?
SET #rownum:=0;
update
set OrderNumber=#rownum:=#rownum + 1
from products
order by Name;
You can simply run an update query with a sequential number like in here
Sounds like a bad design. Why not simply have a plain-jane auto_increment field and order by that? Every new record would by defnition have a higher ID than any of its predecessors.
you mean something like update products set OrderNumber=OrderNumber+1 where Name like 'bla%'

How to store configuration variables in mysql?

M'y script's variables are currently stored in a php File as an array. I want to store them in mysql database, however I don't if I should store them as rows or columns. If I use columns it would be easier to retrieve them (only one query), but if I have too many variables the page would have to scroll horizontally and I think it would be hard to find data in phpmyAdmin. If I use rows then how would I retrieve all of them using a single query and store them in the $config array?
Any ideas or suggestions?
Create config table, with 2 columns
Name | Value
and select it with
SELECT * FROM config
so it will look like
Name | Value
offline | 1
message | Hello guys! This is my custom message
to get them into $config, use
$result = (mysql_query("SELECT * FROM config"));
while($row = mysql_Fetch_assoc($result){
$config[$result['Name']] = $result['Value'];
}
that's it!
Depends.. Are the variables user-based? Do you need to search them? One way to store is in a serialized format (string data in a text field) -- this will suffice if you don't need to search the variables. Otherwise, just store one row per (user-)key-value combination.

adding to numerical value of column

Hi so I was wondering what the best way to add or subtract from a field in my table would be. The way I know it would work is if I query the value do the addition and then UPDATE the value.
I would rather include the addition as part of the update query like:
UPDATE users SET points = +10
Or something like that. Is it possible?
You just need to name the column on the right hand side of the = operator:
UPDATE `users` SET `points` = `points`+10
Since there is no WHERE clause this will give all users 10 more points then they currently have.
You can just do this:
UPDATE users SET points = points + 10

Select MySQL row value as column title?

I'm trying to do a customizable and extendable profile system for my CMS. From the user perspective it is straight forward, but for the admin I want all data, including the profile data, to be searchable. Profile fields may be added by "plugins", which may also add new fields to search on. I don't know if what I'm trying to do with MySQL to make this work is possible or if I'm going at it completely the wrong way.
So I have the users stored in one table (users), with columns for id, email, password and access_level.
I then have another table with profile information (profiles), stored as user_id, parameter and value. The parameter could eventually be put into a separate table again (so it isn't repeating itself), but for now I'll leave it like this.
The parameter and value are basically the profile data. For example, parameter may be "age" and the value may be "22".
What I want to try and do, is select the users table, with the profile information joined so the parameter is mapped to an additional column. So it ends up like so, straight from MySQL:
id email password access_level age
1 a#a.com ***** 1 22
2 b#b.com ***** 2 25
3 c#c.com ***** 2 25
I've been looking at pivot tables all afternoon, but from all I can see the "column name" is pre-defined. In this case I want the "column name" to come from the row itself.
If it isn't possible to do it with a single query, what other methods are there? I'm using PHP if the best method is to do it via that.
Any suggestions would be much appreciated. :)
Well, if you need to know the column names in advance, you can query the information_schema database:
SELECT COLUMN_NAME
FROM information_schema.COLUMNS
WHERE TABLE_NAME='your table'
However, that gets the raw column names. If you're aliasing in your query, you'll have to fetch them indirectly:
SELECT somefield AS alias1, otherfield AS alias2
FROM ...
and then
$stmt = mysql_query($query);
$first = true;
while($row = mysql_fetch_assoc($stmt)) {
if ($first) {
$column_names = array_keys($row);
... display column names here
$first = false;
}
... output row here
}

Categories