Jump to content

Update column with foreach


balkan7

Recommended Posts

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!"; }
}
}
}

 

Link to comment
https://forums.phpfreaks.com/topic/264970-update-column-with-foreach/
Share on other sites

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']."'");
}
?>

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.