silentweed Posted July 3, 2008 Share Posted July 3, 2008 hi everyone - in context this line works fine: $sql = "UPDATE odb_upload SET judge_bean ='".$_POST[$i]."' WHERE id = '".$_POST['record'.$i]."'"; however I want the column name to be dynamic e.g $columname = "judge_bean"; $sql = "UPDATE odb_upload SET $columnname ='".$_POST[$i]."' WHERE id = '".$_POST['record'.$i]."'"; However the above does not work... any ideas? thanks in advance for the help!! Quote Link to comment Share on other sites More sharing options...
bobinindia Posted July 3, 2008 Share Posted July 3, 2008 If i understand you you can set the column name $columnname = $_POST['whatever']; Then the $query Quote Link to comment Share on other sites More sharing options...
silentweed Posted July 3, 2008 Author Share Posted July 3, 2008 thanks but i need to know the actual syntax for the UPDATE statement..if you have a look at my post ..you will see what i mean that is: $columname = "judge_bean"; $sql = "UPDATE odb_upload SET $columnname ='".$_POST[$i]."' WHERE id = '".$_POST['record'.$i]."'"; the above UPDATE syntax needs to be changed...as it doesnt work Quote Link to comment Share on other sites More sharing options...
silentweed Posted July 3, 2008 Author Share Posted July 3, 2008 its ok i've got it: $sql = "UPDATE odb_upload SET ".$columname."='".$_POST[$i]."' WHERE id = '".$_POST['record'.$i]."'"; cheers Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted July 3, 2008 Share Posted July 3, 2008 The syntax in the "reply #2" post is correct. It did not work because you had a spelling error in the variable name. $columname is not equal to $columnname When learning php, developing php code, or debugging php code, turn on full php error reporting (set error_reporting to E_ALL and set display_errors to On) to get php to help you find errors like this one. Quote Link to comment Share on other sites More sharing options...
TransmogriBenno Posted July 3, 2008 Share Posted July 3, 2008 Also, if your variable names are two words, you should use_underscores or useCamelCaps (depending on your preference and the preference of those you share code with, I prefer underscores) so it's easier to read, and it also helps with spelling errors - $thisisnotanicevariablenameisit. Also, I find enclosing variable names in {} to be much nicer on the eyes than switching in and out of strings and concatenating. e.g. "UPDATE odb_upload SET {$column_name} = '{$_POST[$i]}' ..." rather than "UPDATE odb_upload SET ".$column_name."='".$_POST[$i]."' ..." It also means you can include array elements inside strings. Quote Link to comment 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.