imgrooot Posted August 21, 2016 Share Posted August 21, 2016 (edited) Here's what I am trying to do. Retrieve "title" and "slug title" from all the rows in a table. Update them with new "title", "slug title" and "brand name". Here's my query. It updates the table with the brand names for each row. But for some reason, it removes the title and slug title from all the rows in the table. Is there a reason why it's doing that? function getIt($var) { $pos = strpos($var, ' - '); echo(substr($var, $pos+3)."\n"); } if(isset($_POST['submit'])) { $get_record = $db->prepare("SELECT * FROM items"); $get_record->execute(); $result_record = $get_record->fetchAll(PDO::FETCH_ASSOC); if(count($result_record) > 0){ foreach($result_record as $row) { $get_item_id = intval($row['item_id']); $get_item_title = trim($row['item_title']); $get_item_title_slug = trim($row['item_title_slug']); $new_title = getIt($get_item_title); $new_title_slug = Output::createSlug($new_title); $brand_name = substr($get_item_title, 0, strpos($get_item_title, ' - ')); $update_record = $db->prepare("UPDATE items SET brand_name = :brand_name, item_title = :item_title, item_title_slug = :item_title_slug WHERE item_id = :item_id"); $update_record->bindParam(':item_id', $get_item_id); $update_record->bindParam(':brand_name', $brand_name); $update_record->bindParam(':item_title', $new_title); $update_record->bindParam(':item_title_slug', $new_title_slug); if(!$update_record->execute()) { $errors[] = 'There was a problem updating the item.'; } } } else { $errors[] = 'No item found.'; } } Edited August 21, 2016 by imgrooot Quote Link to comment https://forums.phpfreaks.com/topic/301960-quick-help-with-update-query/ Share on other sites More sharing options...
Jacques1 Posted August 21, 2016 Share Posted August 21, 2016 Your getIt() function merely echoes the substring. It doesn't return it, so the value is lost. Quote Link to comment https://forums.phpfreaks.com/topic/301960-quick-help-with-update-query/#findComment-1536426 Share on other sites More sharing options...
Solution imgrooot Posted August 21, 2016 Author Solution Share Posted August 21, 2016 Your getIt() function merely echoes the substring. It doesn't return it, so the value is lost. Ah yes. I did add the return and it now works. function getIt($var) { $pos = strpos($var, ' - '); return substr($var, $pos+3); } Quote Link to comment https://forums.phpfreaks.com/topic/301960-quick-help-with-update-query/#findComment-1536427 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.