Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-13-2013, 06:45 PM   PM User | #1
trackersoftware
New to the CF scene

 
Join Date: Mar 2013
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
trackersoftware is an unknown quantity at this point
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?
trackersoftware is offline   Reply With Quote
Old 03-13-2013, 08:24 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 03-13-2013, 10:37 PM   PM User | #3
trackersoftware
New to the CF scene

 
Join Date: Mar 2013
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
trackersoftware is an unknown quantity at this point
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
trackersoftware is offline   Reply With Quote
Old 03-13-2013, 11:19 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Tags
mysql query, mysql update, sql

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:49 PM.


Advertisement
Log in to turn off these ads.