Jump to content

edit keywords of product


HDFilmMaker2112

Recommended Posts

I have products listed in my database, I need the following to edit that information. The product information itself is fairly straight forward, but when it comes to editing the keywords (for user searches of products), I'm not sure of how to go about it.

 

Here's what I have right now:

<?php
$keywords=$_POST['keyword'];
$keywords=explode(",",$keywords);

$sql20="UPDATE $tbl_name product_name=$product_name, product_price=$product_price, product_category=$product_category, product_link=$product_link, product_image=$product_image, product_tag=$product_tag, product_features=$product_features, product_pros=$product_pros, product_cons=$product_cons, product_description=$product_description, product_notes=$product_notes WHERE product_id = $product_id";
mysql_query($sql20);

$ValuePart = array();
foreach($keywords as $keyword){
$ValuePart[] = "keyword=$keyword";
}

if (count($ValuePart) > 0 ) {
$sql24="UPDATE $tbl_name2 SET" . implode(',',$ValuePart). "WHERE product_id=".$product_id;
mysql_query($sql24) or die(mysql_error());
} 
header("Location: ./admincp.php?edited=yes");

?>

 

Am I going in the wrong direction with the keywords update? It would seem that if I do it the way I have it right now, if I have the following:

a,b,c,d,e,f,g

 

and update it to:

a,c,e,f,g

 

That the database would end up with duplicate entries. How should i combat this? I need a way to delete rows when the update reduces the amount of keywords, add entries when the update increases the amount of keywords, and just alter the existing entries when the amount of keywords stay the same.

 

Link to comment
https://forums.phpfreaks.com/topic/236302-edit-keywords-of-product/
Share on other sites

You need two tables.

 

One for your products of course.

A second for all your keywords

 

In the keywords table you'd have

- an id

- the keyword name

- the product id

 

To find all the keywords for a specific product you will need to JOIN the keywords table to the products.

SELECT * FROM products p
JOIN keywords k ON k.product = p.id

 

To delete a keyword, simply search for it by name and product id

DELETE FROM keywords WHERE keyword = "someword" AND product='5'

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.