MySQL Update query syntax...

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
DynamiteHost
Forum Commoner
Posts: 69
Joined: Sat Aug 10, 2002 5:33 pm

MySQL Update query syntax...

Post by DynamiteHost »

Code: Select all

$query = mysql_query("UPDATE users SET $type='$buyitem', gold='$gold' WHERE id='$usercookie'") or die ("Unable to update equipment.");
When I try to run this I get the "Unable to update equipment." error...

I've tried echoing out $buyitem, $gold, $type and $usercookie and there isn't anything wrong there... the table is definately there also...

thanks :)
dusty
Forum Contributor
Posts: 122
Joined: Sun Apr 28, 2002 9:52 pm
Location: Portsmouth, VA

Post by dusty »

use or die(mysql_error())
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Try this...

Code: Select all

<?php

$query=mysql_query( "UPDATE users SET $type='$buyitem' AND gold='$gold' WHERE id='$usercookie' ") or die ( mysql_error() );

?>
...if that doesn't work make sure all of your variables are set.
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

Code: Select all

<?php
$query = mysql_query("UPDATE users SET `type` = '$buyitem', `gold` = '$gold' WHERE `id` = '$usercookie'") or die (mysql_error());
?>
1st) you had a table column name 'type' as $type, not 'type'.
2nd) put ``s around table column names, and ''s around the columns value
3rd) use mysql_error() in the die();
Image Image
Sunthas
Forum Newbie
Posts: 9
Joined: Sun Dec 08, 2002 12:43 am

Post by Sunthas »

Why would you want to use `s around the table names?
DynamiteHost
Forum Commoner
Posts: 69
Joined: Sat Aug 10, 2002 5:33 pm

Post by DynamiteHost »

it's supposed to be $type as I mentioned in my first post which has nothing to do with the error as I have tried it with and without.
User avatar
puckeye
Forum Contributor
Posts: 105
Joined: Fri Dec 06, 2002 7:26 pm
Location: Joliette, QC, CA
Contact:

Re: MySQL Update query syntax...

Post by puckeye »

DynamiteHost wrote:

Code: Select all

$query = mysql_query("UPDATE users SET $type='$buyitem', gold='$gold' WHERE id='$usercookie'") or die ("Unable to update equipment.");
When I try to run this I get the "Unable to update equipment." error...

I've tried echoing out $buyitem, $gold, $type and $usercookie and there isn't anything wrong there... the table is definately there also...

thanks :)
HI,

What I like to do when querying is to separate the query string and the mysql_query command like so:

Code: Select all

<?php
$query = "UPDATE users SET $type='$buyitem', gold='$gold' WHERE id='$usercookie'";
$result = mysql_query($query) or print mysql_error()."<BR>".$query;

?>
That way using or print mysql_error()."<BR>".$query I can easily find what was the error and if PHP somehow misinterpreted my query string.

Puckeye
Post Reply