I am trying to update for some products their category in database. I
want to find products that have in their name a specific word and after
that I want to update the category for this products.
I have to table sho_posts and sho_term_relationships. Firs of all I what
to extract ID’s for each item from table sho_posts where post_title
contain in it as a part words “Audio CD”. After that I want to make an
update in table sho_term_relationships at field term_taxonomy_id with
some value for example “2” where object_id from sho_term_relationships
is equal with ID from sho_posts that I have selected earlier.
I wrote a little PHP code but it make only selection part. What is
wrong? my script change only one row from 4 rows, the last one.
$username = “_shoping”;
$password = “password”;
$hostname = “localhost”;
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die(“Unable to connect to MySQL”);
$selected = mysql_select_db(“_shoping”,$dbhandle)
or die(“Could not select examples”);
echo “Connected to MySQL
”;
$result = mysql_query ("SELECTid
FROMsho_posts
WHERE CONVERT(post_title
USING utf8) LIKE ‘%Audio CD%’ ");
while ($row = mysql_fetch_array($result)) {
$id = $row[‘id’];
/echo “ID2:”.$id.“
”;/
}
foreach ($id as $value) {
echo “value:”.$value.“
”;
}
/$id = $row[‘id’];/
$sql = “UPDATE sho_term_relationships
SET term_taxonomy_id = ‘123’
WHERE object_id =”.$value;
mysql_query($sql);
//close the connection
mysql_close($dbhandle);