willwill100 Posted March 22, 2006 Share Posted March 22, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/5480-update-query-help/ Share on other sites More sharing options...
wisewood Posted March 22, 2006 Share Posted March 22, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/5480-update-query-help/#findComment-19606 Share on other sites More sharing options...
wickning1 Posted March 22, 2006 Share Posted March 22, 2006 You can't put table and column names in '' quotes. You have to use `` backticks or MySQL gets confused. Quote Link to comment https://forums.phpfreaks.com/topic/5480-update-query-help/#findComment-19636 Share on other sites More sharing options...
craygo Posted March 22, 2006 Share Posted March 22, 2006 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 Link to comment https://forums.phpfreaks.com/topic/5480-update-query-help/#findComment-19644 Share on other sites More sharing options...
willwill100 Posted March 22, 2006 Author Share Posted March 22, 2006 i have declared '$compl' earlier, thats not the problemwickning1, 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 columnecho ($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?? Quote Link to comment https://forums.phpfreaks.com/topic/5480-update-query-help/#findComment-19706 Share on other sites More sharing options...
lessthanthree Posted March 22, 2006 Share Posted March 22, 2006 [!--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.. Quote Link to comment https://forums.phpfreaks.com/topic/5480-update-query-help/#findComment-19707 Share on other sites More sharing options...
willwill100 Posted March 22, 2006 Author Share Posted March 22, 2006 ny1 help?? Quote Link to comment https://forums.phpfreaks.com/topic/5480-update-query-help/#findComment-19713 Share on other sites More sharing options...
willwill100 Posted March 22, 2006 Author Share Posted March 22, 2006 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 advanceWill Quote Link to comment https://forums.phpfreaks.com/topic/5480-update-query-help/#findComment-19727 Share on other sites More sharing options...
craygo Posted March 22, 2006 Share Posted March 22, 2006 Backticks are killing me :pTry 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 Quote Link to comment https://forums.phpfreaks.com/topic/5480-update-query-help/#findComment-19744 Share on other sites More sharing options...
willwill100 Posted March 22, 2006 Author Share Posted March 22, 2006 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.. Quote Link to comment https://forums.phpfreaks.com/topic/5480-update-query-help/#findComment-19765 Share on other sites More sharing options...
wickning1 Posted March 22, 2006 Share Posted March 22, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/5480-update-query-help/#findComment-19768 Share on other sites More sharing options...
willwill100 Posted March 22, 2006 Author Share Posted March 22, 2006 22/03/06 Race 1i have used it in phpmyadmin and it returns a positive resultany more ideas? Quote Link to comment https://forums.phpfreaks.com/topic/5480-update-query-help/#findComment-19772 Share on other sites More sharing options...
wickning1 Posted March 22, 2006 Share Posted March 22, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/5480-update-query-help/#findComment-19779 Share on other sites More sharing options...
willwill100 Posted March 23, 2006 Author Share Posted March 23, 2006 thanx for the help, it works now Quote Link to comment https://forums.phpfreaks.com/topic/5480-update-query-help/#findComment-19861 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.