MySQL query doesnt work

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

adamb10
Forum Commoner
Posts: 91
Joined: Sat Jun 24, 2006 7:44 pm

MySQL query doesnt work

Post by adamb10 »

Code: Select all

mysql_query ("UPDATE settings SET 'title'='".$title."', 'username'='".$name."', 'email'='". $email."', 'tablewidth'='".$width."', 'menubar'='".$menubar."', 'postorder'='".postorder."', 'posting'='".$posting."', 'kwikpost'='".$kwikpost."', 'maxshow'='".$maxshow."', 'im'='".$im."', 'links'='".$links."', 'logo'='".$logo."', 'menubarlocation'='".$menubarlocation."', 'kwikpostlocation'='".$kwikpostlocation."'");
That query doesnt do anything, I dunno whats wrong with it. :(
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

Code: Select all

$q = $mysql_query($sql) or print(mysql_error());
do error checking and read the manual
User avatar
kbrown3074
Forum Contributor
Posts: 119
Joined: Thu Jul 20, 2006 1:36 pm

Post by kbrown3074 »

I see you have single quotes around your field names..not necessary. Also try putting just single quotes around your vars....see below Also..kinda disturbing..you will set every record to these values since you dont have a where clause. Also you missed a $ on the posting var

Code: Select all

mysql_query ("UPDATE settings SET title='$title', username='$name', email='$email', tablewidth='$width', menubar='$menubar', postorder='$postorder', posting='$posting', kwikpost='$kwikpost', maxshow='$maxshow', im='$im', links='$links', logo='$logo',menubarlocation='$menubarlocation', kwikpostlocation='$kwikpostlocation'");
[/url]
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

single quotes are fine for fields that are strings and integers
adamb10
Forum Commoner
Posts: 91
Joined: Sat Jun 24, 2006 7:44 pm

Post by adamb10 »

I did try errors on and MySQL printed...

Code: Select all

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 ''title'='Testbook', 'username'='adam', 'email'='debailey@wi.rr.com', 'tablewidth' at line 1
I fixed the variable where no $ was btw.
jamiel
Forum Contributor
Posts: 276
Joined: Wed Feb 22, 2006 5:17 am
Location: London, United Kingdom

Post by jamiel »

Your field names need to have backticks (`) around them not quotes (')
adamb10
Forum Commoner
Posts: 91
Joined: Sat Jun 24, 2006 7:44 pm

Post by adamb10 »

Backticks didnt work either. :p I hate being a n00b at mysql.
jamiel
Forum Contributor
Posts: 276
Joined: Wed Feb 22, 2006 5:17 am
Location: London, United Kingdom

Post by jamiel »

Put all that sql into a variable called $sql .. then print_r($sql) just before the query.

Show us the output of that aswell as the new mysql_error() you are most likely getting.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

sorry kbrown and jamiel, didnt notice that he was single quoting field names and didn't mean to go against what you two said.
adamb10
Forum Commoner
Posts: 91
Joined: Sat Jun 24, 2006 7:44 pm

Post by adamb10 »

Code: Select all

print_r($sql)
    $sql = "mysql_query ("UPDATE settings WHERE `title`='".$title."', `username`='".$name."', `email`='". $email."', `tablewidth`='".$width."', `menubar`='".$menubar."', `postorder`='".$postorder."', `posting`='".$posting."', `kwikpost`='".$kwikpost."', `maxshow`='".$maxshow."', `im`='".$im."', `links`='".$links."', `logo`='".$logo."', `menubarlocation`='".$menubarlocation."', `kwikpostlocation`='".$kwikpostlocation."'") or print(mysql_error())";
The variable has a parse error in it. I usually dont apply variables to big queries like this.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

you have to end every line with a semi colon ;/b]

Code: Select all

print("<pre>");
print_r($sql);
print("</pre>");
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

adamb10 wrote:

Code: Select all

$sql = "UPDATE settings WHERE `title`='".$title."', `username`='".$name."', `email`='". $email."', `tablewidth`='".$width."', `menubar`='".$menubar."', `postorder`='".$postorder."', `posting`='".$posting."', `kwikpost`='".$kwikpost."', `maxshow`='".$maxshow."', `im`='".$im."', `links`='".$links."', `logo`='".$logo."', `menubarlocation`='".$menubarlocation."', `kwikpostlocation`='".$kwikpostlocation."'";
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

He needs to escape the vars. The '@' might be giving him problems.
adamb10
Forum Commoner
Posts: 91
Joined: Sat Jun 24, 2006 7:44 pm

Post by adamb10 »

MySQL didnt return an error that time but the query is still dead in the water.. :(
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

What do you mean "dead in the water"?

Code: Select all

$r = mysql_query($sql);

print mysql_affected_rows($r);
Post Reply