HDFilmMaker2112 Posted May 13, 2011 Share Posted May 13, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236302-edit-keywords-of-product/ Share on other sites More sharing options...
HDFilmMaker2112 Posted May 13, 2011 Author Share Posted May 13, 2011 Anybody at all? Quote Link to comment https://forums.phpfreaks.com/topic/236302-edit-keywords-of-product/#findComment-1215169 Share on other sites More sharing options...
Zane Posted May 14, 2011 Share Posted May 14, 2011 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' Quote Link to comment https://forums.phpfreaks.com/topic/236302-edit-keywords-of-product/#findComment-1215333 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.