Jump to content


Photo

update query help


  • Please log in to reply
13 replies to this topic

#1 willwill100

willwill100
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 22 March 2006 - 08:07 AM

my update query will not work:

$q= "SELECT `Name` , `Position` FROM `temp`";
$result = mysql_query ($q) or die('Problem with query: ' . $q . '<br />' . mysql_error());

while ($row = mysql_fetch_assoc($result)){
    
    $name=$row['Name'];
    $pos=$row['Position'];
    
$query = mysql_query("UPDATE '$compl' SET '$colnom' = '$pos' WHERE `Name`='$name'") or die(mysql_error() . $query);
echo ("Updated sailor: " . $name . "<br>");
}

it just returns the error:

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 ''topper main points' '22/03/06 Race 1' = '2' WHERE CONVERT( `Name` USING utf8 ) ' at line 1

what could be wrong with the query?

#2 wisewood

wisewood
  • Members
  • PipPipPip
  • Advanced Member
  • 226 posts
  • LocationRotherham, England

Posted 22 March 2006 - 10:55 AM

UPDATE '[!--coloro:#FF6666--][span style=\"color:#FF6666\"][!--/coloro--]$compl' [!--colorc--][/span][!--/colorc--]SET '$colnom' = '$pos' WHERE `Name`='$name'")


I am not seeing anywhere where you are declaring what $compl is.
If it doesnt know what $compl is supposed to be, then the update query doesnt know what table it is meant to update. hope this helps.
wisewood: proven fact, I am both wise, and wooden.

#3 wickning1

wickning1
  • Members
  • PipPipPip
  • Advanced Member
  • 405 posts

Posted 22 March 2006 - 01:06 PM

You can't put table and column names in '' quotes. You have to use `` backticks or MySQL gets confused.

#4 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 22 March 2006 - 01:46 PM

There is really no need to put backticks at all unless your tables or field names contain spaces. You shouldn't put spaces in them anyway, but to each his own. Underscores work alot better and help making your code look cleaner.

Ray



#5 willwill100

willwill100
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 22 March 2006 - 06:09 PM

i have declared '$compl' earlier, thats not the problem

wickning1, my col names and table name have been changed to the `` and it has made some progress.

Now when i send $name to be "Stewart Brown" i get this error:

Unknown column 'Stewart Brown' in 'where clause'

i dont see why it should do this since i am declaring '$colnom' as the column name.

Heres my latest full code:

$comp = $_REQUEST['comp'];
$tdate = date('j/m/y');//Get todays date
$raceno = $_REQUEST['raceno'];
$compl = strtolower($comp);
$colnom = ($tdate . " Race " . $raceno); //Find Name for column
echo ($colnom);

$q= "SELECT `Name` , `Position` FROM `temp`";
$result = mysql_query ($q) or die('Problem with query: ' . $q . '<br />' . mysql_error());

while ($row = mysql_fetch_assoc($result)){
    
    $name=$row['Name'];
    $pos=$row['Position'];
    
$query = mysql_query("UPDATE `$compl` SET `$colnom` = `$pos` WHERE `Name`=`$name`") or die(mysql_error() . $query);
echo ("Updated sailor: " . $name . "<br>");
}

any ideas as to the problem??

#6 lessthanthree

lessthanthree
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts
  • LocationUK

Posted 22 March 2006 - 06:15 PM

[!--quoteo(post=357282:date=Mar 22 2006, 01:46 PM:name=craygo)--][div class=\'quotetop\']QUOTE(craygo @ Mar 22 2006, 01:46 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
There is really no need to put backticks at all unless your tables or field names contain spaces. You shouldn't put spaces in them anyway, but to each his own. Underscores work alot better and help making your code look cleaner.

Ray
[/quote]

You also HAVE to use backticks if your field names are reserved mysql words..

call me a safe bet, i'm betting i'm not

#7 willwill100

willwill100
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 22 March 2006 - 06:41 PM

ny1 help??

#8 willwill100

willwill100
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 22 March 2006 - 07:35 PM

BUMP I really need help with this, I have to have the system working by tomoro! Could someone give this query some time?

Thanks in advance
Will

#9 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 22 March 2006 - 08:41 PM

Backticks are killing me :P

Try this out
$q= "SELECT Name, Position FROM temp";
$result = mysql_query ($q) or die('Problem with query: ' . $q . '<br />' . mysql_error());

while ($row = mysql_fetch_assoc($result)){
    
    $name=$row['Name'];
    $pos=$row['Position'];
    
$query = mysql_query("UPDATE $compl SET $colnom = '$pos' WHERE Name = '$name'") or die(mysql_error() . $query);
echo ("Updated sailor: " . $name . "<br>");
}

Looks like you are creating a new column name everytime you run the script??
$colnom = ($tdate . " Race " . $raceno); //Find Name for column
Not going to work if the column name does not exist.

What are you trying to do anyway??

Ray



#10 willwill100

willwill100
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 22 March 2006 - 10:05 PM

with the code you gave me i get the error:

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 'points SET 22/03/06 Race 1 = '1' WHERE Name = 'Will Williams'' at line 1

No, im not creating a new column im just inserting into a column that was just created. It's a sailing system that is supposed to transfer what I have in temp into the proper results table with the corrects positions etc..



#11 wickning1

wickning1
  • Members
  • PipPipPip
  • Advanced Member
  • 405 posts

Posted 22 March 2006 - 10:10 PM

SET 22/03/06 Race 1 = '1'

`22/03/06 Race 1` is not a valid column name. What is the name of the column you are trying to update?

#12 willwill100

willwill100
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 22 March 2006 - 10:23 PM

22/03/06 Race 1

i have used it in phpmyadmin and it returns a positive result

any more ideas?

#13 wickning1

wickning1
  • Members
  • PipPipPip
  • Advanced Member
  • 405 posts

Posted 22 March 2006 - 10:49 PM

Wow, if that really is the column name, you have a very bad database design. You should find someone to help you redesign it and port the data over.

For now this should work for you:

$query = mysql_query("UPDATE `$compl` SET `$colnom` = '$pos' WHERE `Name`='$name'") or die(mysql_error() . $query);
Just remember - backticks `` for tables and columns, single quotes '' for string values.

#14 willwill100

willwill100
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 23 March 2006 - 07:51 AM

thanx for the help, it works now




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users