gamesmstr Posted November 23, 2008 Share Posted November 23, 2008 I have a variable $type which is a string. I am trying to update a db table where an element matches that string and increment it by 1. I am using the following format. mysql_query("UPDATE tablename set '$type'='$type'+1 where id = '$playerid'"); This format is not working and I suspect the culprit is the set '$type'='$type'+1. Can anyone help me on how it should be formatted? Quote Link to comment https://forums.phpfreaks.com/topic/133916-solved-formatting-question/ Share on other sites More sharing options...
wildteen88 Posted November 23, 2008 Share Posted November 23, 2008 The format for your query should be mysql_query("UPDATE tablename set column_name=column_name+1 where id = '$playerid'"); Does $type hold a column name? Quote Link to comment https://forums.phpfreaks.com/topic/133916-solved-formatting-question/#findComment-697085 Share on other sites More sharing options...
Mchl Posted November 23, 2008 Share Posted November 23, 2008 It's not clear what you want Maybe this: mysql_query("UPDATE tablename set 'type'='type'+1 where id = '$playerid'"); Or maybe this: mysql_query("UPDATE tablename set 'type'=$type+1 where id = '$playerid'"); Does $type hold a column name? Good question. Didn't think of that Quote Link to comment https://forums.phpfreaks.com/topic/133916-solved-formatting-question/#findComment-697086 Share on other sites More sharing options...
premiso Posted November 23, 2008 Share Posted November 23, 2008 You need to use backticks (`) around column names and single quotes(') around data. mysql_query("UPDATE tablename set `$type`= ($type+1) where id = '$playerid'"); Also does $type hold a column name? (oh I see thats already been pointed out oh well.) Quote Link to comment https://forums.phpfreaks.com/topic/133916-solved-formatting-question/#findComment-697088 Share on other sites More sharing options...
gamesmstr Posted November 23, 2008 Author Share Posted November 23, 2008 The format for your query should be mysql_query("UPDATE tablename set column_name=column_name+1 where id = '$playerid'"); Does $type hold a column name? Yes, in string format. Quote Link to comment https://forums.phpfreaks.com/topic/133916-solved-formatting-question/#findComment-697091 Share on other sites More sharing options...
Mchl Posted November 23, 2008 Share Posted November 23, 2008 If there's column name in $type, then updating the column to its name +1 has little sense... mysql_query("UPDATE tablename set `$type`=`$type`+1 where id = '$playerid'"); Quote Link to comment https://forums.phpfreaks.com/topic/133916-solved-formatting-question/#findComment-697092 Share on other sites More sharing options...
gamesmstr Posted November 23, 2008 Author Share Posted November 23, 2008 If there's column name in $type, then updating the column to its name +1 has little sense... mysql_query("UPDATE tablename set `$type`=`$type`+1 where id = '$playerid'"); Let me give an example $type="cname"; I want the equivalent of mysql_query("UPDATE tablename set cname=cname+1 where id = '$playerid'"); but using the $type variable. Quote Link to comment https://forums.phpfreaks.com/topic/133916-solved-formatting-question/#findComment-697099 Share on other sites More sharing options...
premiso Posted November 23, 2008 Share Posted November 23, 2008 $type_data = $type . "1"; mysql_query("UPDATE tablename set `$type`= '$type_data' where id = '$playerid'"); or mysql_query("UPDATE tablename set `$type`= '" . $type . "1' where id = '$playerid'"); Should do the trick. Quote Link to comment https://forums.phpfreaks.com/topic/133916-solved-formatting-question/#findComment-697105 Share on other sites More sharing options...
Mchl Posted November 23, 2008 Share Posted November 23, 2008 The code I posted above should work. Quote Link to comment https://forums.phpfreaks.com/topic/133916-solved-formatting-question/#findComment-697111 Share on other sites More sharing options...
gamesmstr Posted November 23, 2008 Author Share Posted November 23, 2008 $type_data = $type . "1"; mysql_query("UPDATE tablename set `$type`= '$type_data' where id = '$playerid'"); or mysql_query("UPDATE tablename set `$type`= '" . $type . "1' where id = '$playerid'"); Should do the trick. What I am trying to do is update column $type with the value contained in that column + 1 Quote Link to comment https://forums.phpfreaks.com/topic/133916-solved-formatting-question/#findComment-697117 Share on other sites More sharing options...
gamesmstr Posted November 23, 2008 Author Share Posted November 23, 2008 The code I posted above should work. It did... Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/133916-solved-formatting-question/#findComment-697123 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.