...

View Full Version : UPDATE query does not work in php but with work in phpmyadmin



trackersoftware
03-13-2013, 06:45 PM
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?

Old Pedant
03-13-2013, 08:24 PM
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

trackersoftware
03-13-2013, 10:37 PM
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.



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

Old Pedant
03-13-2013, 11:19 PM
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:


$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?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum