MySQL UPDATE issues [SOLVED]

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
the9ulaire
Forum Commoner
Posts: 74
Joined: Mon Jun 11, 2007 11:31 am

MySQL UPDATE issues [SOLVED]

Post by the9ulaire »

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Here's my code:

Code: Select all

if($_POST['type'] == 'main') {
 
    $sql = "UPDATE main_pages " .
           "SET name='" . $_POST['page_name'] .
           "', desc='" . $_POST['page_desc'] .
           "', content='" . $_POST['page_content'] .
           "' WHERE id=" . $_POST['page_id'];
           
} else if ($_POST['type'] == 'sub') {
 
    $sql = "UPDATE sub_pages " .
           "SET sub_name='" . $_POST['page_name'] .
           "', sub_desc='" . $_POST['page_desc'] .
           "', sub_content='" . $_POST['page_content'] .
           "' WHERE sub_id=" . $_POST['page_id'];
           
}
mysql_query($sql, $conn) or die('Could not update page; ' . mysql_error());
If the transaction is set at 'sub' it works perfectly, however when it is 'main' it gives me an error:
Could not update page; You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc='H', content='t' WHERE id='1'' at line 1
I have no idea what my problem is... Any ideas?

Thanks!
luke


~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
Last edited by the9ulaire on Sun Apr 13, 2008 1:12 pm, edited 1 time in total.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: MySQL UPDATE issues

Post by pickle »

Print out the query & see what it looks like.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
the9ulaire
Forum Commoner
Posts: 74
Joined: Mon Jun 11, 2007 11:31 am

Re: MySQL UPDATE issues

Post by the9ulaire »

UPDATE main_pages SET name='t', desc='t', content='t' WHERE id=1Could not update page; You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc='t', content='t' WHERE id=1' at line 1
the9ulaire
Forum Commoner
Posts: 74
Joined: Mon Jun 11, 2007 11:31 am

Re: MySQL UPDATE issues

Post by the9ulaire »

I just echoed my $sql statement with the above
User avatar
EverLearning
Forum Contributor
Posts: 282
Joined: Sat Feb 23, 2008 3:49 am
Location: Niš, Serbia

Re: MySQL UPDATE issues

Post by EverLearning »

DESC is a reserved word in MySql, so it doesn't see it as a field name, but something that shouldn't be there :)
To make your statement work, surround your field names with backticks:

Code: Select all

UPDATE main_pages
SET `name` = 't', `desc` = 't', `content` = 't' 
WHERE `id` =1
the9ulaire
Forum Commoner
Posts: 74
Joined: Mon Jun 11, 2007 11:31 am

Re: MySQL UPDATE issues

Post by the9ulaire »

Thank you SO much for pointing that out to me!

I tried putting it in single quotes but it still didn't work. I instead went in and changed all my column names and gave them the prefix main_ and now everything works perfectly!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: MySQL UPDATE issues

Post by John Cartwright »

the9ulaire wrote: I tried putting it in single quotes but it still didn't work.
backticks, not single quotes

` vs '
the9ulaire
Forum Commoner
Posts: 74
Joined: Mon Jun 11, 2007 11:31 am

Re: MySQL UPDATE issues

Post by the9ulaire »

Jcart wrote:
the9ulaire wrote: I tried putting it in single quotes but it still didn't work.
backticks, not single quotes

` vs '
Ohhhhhh, okay. Good to know. Thank you!
Post Reply