I need to copy description of the product from one table to another, everything works fine until the <img src="url" width="50%" height="50%" />
tag appears in the description. I have a script that copying descriptions of product one by one, with this script in cycle:
$rowParse = Parsing::find()->where(['art_post' => $value])->one();
$desc = $rowParse->description;
Yii::$app->db->createCommand("UPDATE `products` SET `description`=:desc WHERE `id_post`=:id && `art_post`=:art")
->bindValues([':desc'=>$desc, ':id'=>$id_post, ':art'=>$art_post])->execute();
If I do copying description of single product it works as needed – tag inserts with his attributes, but if i do, for example, 100 products in cycle, attributes width
and height
just disappears and it inserts just like <img src="url" />
Insertion of all other html-tags like span, strong, td, tr, p works normal.
I’m new to programming world, so I need help of big guys 😀
Thank you in advance
2
Answers
Ensure that the
description
field in theproducts
table has a data type that supports storing HTML, such asTEXT
orLONGTEXT
. If the field is of a type likeVARCHAR
with a limited length, it might truncate your HTML content.Make sure that when you retrieve the description from the
Parsing
table, the HTML content is not being encoded or escaped. Yii might automatically escape HTML entities when retrieving data from the database. You can useHtml::decode()
to ensure that the HTML is not encoded:If you are updating a large number of records in a loop, you might want to consider wrapping your updates in a transaction to ensure data consistency:
Add some logging statements to your code to see what values are being retrieved and updated. This can help you identify where the issue is occurring.
You can then check the logs to see if the width and height attributes are present at the time of update.
There is no need to read the values into PHP/Yii, and then write them back to the database, unless you are doing some complex manipulation in between. MySQL can do this for you much more efficiently.
It is not clear from your question what the values of
$value
,$id_post
and$art_post
are or where they are coming from, but it seems likely this can be accomplished with a multi-table update. Something like: