Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    UPDATE query does not work in php but with work in phpmyadmin

    I have been trying to get my update statement to work,

    Code:

    $newalias = strtolower(trim(str_replace(" ", "-", $itemName))); // Trim all spaces, remove spaces with dash and lowercase only
    $sql="UPDATE {$this->tableNameDB} SET alias = {$newalias} WHERE id = {$id}";
    $ret=$this->db->query($sql);

    The update query works in phpmysql but not in php. The code takes the item name, replaces the spaces with dashes and converts to lower case before setting the alias in the query.

    Here is the echoed SQL statement:
    UPDATE dt_Products SET alias = new-product WHERE id = 21

    Works in phpmyadmin and there is an identical update in a previous function that worked but not this one. There were no errors reported.

    Any reason why it's not updating?

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Wrong:
    UPDATE dt_Products SET alias = new-product WHERE id = 21

    Right:
    UPDATE dt_Products SET alias = 'new-product' WHERE id = 21

    Missing apostrophes around 'new-product' of course.

    I doubt seriously that the query, as you gave it, would work in phpmysql.

    If new-product is the name of a column in that table (seems unlikely but I guess it could be) then you need back ticks around the name:
    UPDATE dt_Products SET alias = `new-product` WHERE id = 21
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    tried it and didn't update. The query before did update in phpmysql.

    The new-product is a field value just as an example, it takes the item name which is new product and adds the dash(s) before updating. Used in the url on the page for the item name. I have read that an update sql can cause problems especially with the id. id is the product number that exists.

    Quote Originally Posted by Old Pedant View Post
    Right:
    UPDATE dt_Products SET alias = 'new-product' WHERE id = 21

    Missing apostrophes around 'new-product' of course.

    I doubt seriously that the query, as you gave it, would work in phpmysql.


    If new-product is the name of a column in that table (seems unlikely but I guess it could be) then you need back ticks around the name:
    UPDATE dt_Products SET alias = `new-product` WHERE id = 21

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Sorry, but I still think you are wrong.

    You said you "tried and it didn't update". *WHAT* did you try.

    You should have changed your PHP code to something like this:
    Code:
    $sql="UPDATE {$this->tableNameDB} SET alias = '{$newalias}' WHERE id = {$id}";
    See the apostrophes around {$newalias}??

    I have to ask: WHY do you need to use {$this->tableNameDB}? SURELY you don't have multiple tables that this same query could be applied to?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •