Jump to content

Archived

This topic is now archived and is closed to further replies.

willwill100

update query help

Recommended Posts

my update query will not work:

[code]$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>");
}[/code]

it just returns the error:

[code]
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[/code]

what could be wrong with the query?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
You can't put table and column names in '' quotes. You have to use `` backticks or MySQL gets confused.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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:

[code]Unknown column 'Stewart Brown' in 'where clause'[/code]

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

Heres my latest full code:

[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>");
}
[/code]

any ideas as to the problem??

Share this post


Link to post
Share on other sites
[!--quoteo(post=357282:date=Mar 22 2006, 01:46 PM:name=craygo)--][div class=\'quotetop\']QUOTE(craygo @ Mar 22 2006, 01:46 PM) [snapback]357282[/snapback][/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..

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Backticks are killing me :p

Try this out
[code]$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>");
}[/code]

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

What are you trying to do anyway??

Ray

Share this post


Link to post
Share on other sites
with the code you gave me i get the error:

[code]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[/code]

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..

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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:

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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.