Page 1 of 1

MySQL UPDATE issues [SOLVED]

Posted: Fri Apr 11, 2008 12:31 pm
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.

Re: MySQL UPDATE issues

Posted: Fri Apr 11, 2008 3:17 pm
by pickle
Print out the query & see what it looks like.

Re: MySQL UPDATE issues

Posted: Fri Apr 11, 2008 4:22 pm
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

Re: MySQL UPDATE issues

Posted: Fri Apr 11, 2008 4:23 pm
by the9ulaire
I just echoed my $sql statement with the above

Re: MySQL UPDATE issues

Posted: Sat Apr 12, 2008 8:56 am
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

Re: MySQL UPDATE issues

Posted: Sun Apr 13, 2008 1:11 pm
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!

Re: MySQL UPDATE issues

Posted: Sun Apr 13, 2008 5:43 pm
by John Cartwright
the9ulaire wrote: I tried putting it in single quotes but it still didn't work.
backticks, not single quotes

` vs '

Re: MySQL UPDATE issues

Posted: Sun Apr 13, 2008 5:51 pm
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!