balkan7 Posted June 28, 2012 Share Posted June 28, 2012 I want to update empty columns in datebase, so i have this table: category - cid - cname - cparent - cseo Column cseo is empty i want update it with cname column with seo function. This foreach doesn't work code is: <?php $cats = mysql_query("SELECT * FROM category WHERE cparent ='4' ORDER BY cid DESC"); while ($data = mysql_fetch_array($cats)) { if ($data['cseo'] == ""){ foreach ($data['cname'] as $seo_names) { $seo_link = seo($seo_names); $result = mysql_query("UPDATE category SET cseo='$seo_link' WHERE cid='".$data['cid']."'"); if ($result) { echo "Done!"; } } } } Quote Link to comment https://forums.phpfreaks.com/topic/264970-update-column-with-foreach/ Share on other sites More sharing options...
Drummin Posted June 28, 2012 Share Posted June 28, 2012 Correct, $data['cname'] is not an array. What is seo($seo_names)? Quote Link to comment https://forums.phpfreaks.com/topic/264970-update-column-with-foreach/#findComment-1357822 Share on other sites More sharing options...
balkan7 Posted June 28, 2012 Author Share Posted June 28, 2012 Ok i have seo function for replace names from category like this: cname: Foo Bar cseo: foo-bar so i don't know which is the best method to update every cname column to cseo column Quote Link to comment https://forums.phpfreaks.com/topic/264970-update-column-with-foreach/#findComment-1357825 Share on other sites More sharing options...
Drummin Posted June 28, 2012 Share Posted June 28, 2012 Seems like this should work. <?php $cats = mysql_query("SELECT cid, cname FROM category WHERE cparent ='4' ORDER BY cid DESC"); while ($data = mysql_fetch_array($cats)) { $cname = $data['cname']; $seo_link = seo($cname); $result = mysql_query("UPDATE category SET cseo='$seo_link' WHERE cid='".$data['cid']."'"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/264970-update-column-with-foreach/#findComment-1357829 Share on other sites More sharing options...
balkan7 Posted June 28, 2012 Author Share Posted June 28, 2012 Yes Working Thanks ! Quote Link to comment https://forums.phpfreaks.com/topic/264970-update-column-with-foreach/#findComment-1357830 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.