Jump to content

Quick help with update query


Go to solution Solved by imgrooot,

Recommended Posts

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 by imgrooot
Link to post
Share on other sites
  • Solution

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);
}
Link to post
Share on other sites
This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.