Badboy8 Posted January 27, 2008 Share Posted January 27, 2008 Halp I need your help, my php code is not working. Ok ok I have a small PHP-site that I try to work with, and here's the following I need help with: form sends _POST variables $a_1, $a_2, $a_3... etc. to my .php page which should update my mysql tables with these variables. Now to teh problem, i know how to update tables manually like: foreach ($_POST as $key=>$val){ $$key = $val; } $sql = "UPDATE abc SET a_1='$a_1',a_2='$a_2',a_3='$a_3' WHERE id='$id'" mysql_query($sql,$db); how can i get this to work automatically without adding all the a_1='a_1' stuff in there? the following doesn't work: foreach ($_POST as $key=>$val){ $$key = $val; } $poop=1; while($poop<4) { $sql = "UPDATE abc SET a_$poop='$a_$poop' WHERE id='$id'" mysql_query($sql,$db); $poop++; } AND this doesn't work either foreach ($_POST as $key=>$val){ $$key = $val; } $poop=1; while($poop<4) { $dodo="a_".$poop; $sql = "UPDATE abc SET $dodo='$dodo' WHERE id='$id'" mysql_query($sql,$db); $poop++; } Please halp and... thanks for halping. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted January 27, 2008 Share Posted January 27, 2008 Would be nice if you told us what you meant by 'not working'. However, i'd do something like: <?php $update = array(); for($x=1;$x<4;$x++){ $field = 'a_'.$x; $value = $_POST[$field]; $update[] = "$field ='$value'"; } $update_str = implode(',',$update); $sql = "UPDATE abc SET $update_str WHERE id='$id'"; mysql_query($sql) or die(mysql_error($sql.'<br />Query:'.$sql)); ?> Quote Link to comment Share on other sites More sharing options...
Badboy8 Posted January 27, 2008 Author Share Posted January 27, 2008 My code works, but by saying my code doesn't work I mean that the loops posted under the manual loop-thing doesn't work. (as it should anyway) Thank you for the for-loop, but is there a way to do the loop with where-clause? I'm asking because my understanding of PHP is not enough to defuse arrays and for-clauses yet So is there a way to get this to work with while-clause as below? foreach ($_POST as $key=>$val){ $$key = $val; } $poop=1; while($poop<4) { $dodo="a_".$poop; $sql = "UPDATE abc SET $dodo='$dodo' WHERE id='$id'" mysql_query($sql,$db); $poop++; } Is there a way to make a variable from e.g. two variables so it would work as a variable itself? like: $a=$a_; $b=1; $dodo = $a.$b; $poop=1; while($poop<4) { $sql = "UPDATE abc SET $dodo='$dodo' WHERE id='$id'" mysql_query($sql,$db); $poop++; } Here I would need the $dodo to work as $a_1 - when used with while-clause the $dodo should work like $a_1, $a_2, $a_3... etc. Quote Link to comment Share on other sites More sharing options...
tibberous Posted January 27, 2008 Share Posted January 27, 2008 Is there a way to make a variable from e.g. two variables so it would work as a variable itself? Yeah - the concept is called variable variables, PHP is one of few languages that let you do it - you are actually using it in the code you have there: $$key = $val; There awesome: www.php.net/language.variables.variable Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted January 28, 2008 Share Posted January 28, 2008 Try: foreach ($_POST as $key=>$val){ $$key = $val; } $poop=1; while($poop<4) { $dodo="a_".$poop; $sql = "UPDATE abc SET $dodo='$$dodo' WHERE id='$id'" mysql_query($sql,$db) or die(mysql_error()); $poop++; } As was said above, you need to be using another variable variable, It is needed in this line: $sql = "UPDATE abc SET $dodo='$$dodo' WHERE id='$id'" Since you want to be updating the field, not with a_1 for example, but with the value of $a_1 Quote Link to comment Share on other sites More sharing options...
Badboy8 Posted January 28, 2008 Author Share Posted January 28, 2008 Try: foreach ($_POST as $key=>$val){ $$key = $val; } $poop=1; while($poop<4) { $dodo="a_".$poop; $sql = "UPDATE abc SET $dodo='$$dodo' WHERE id='$id'" mysql_query($sql,$db) or die(mysql_error()); $poop++; } As was said above, you need to be using another variable variable, It is needed in this line: $sql = "UPDATE abc SET $dodo='$$dodo' WHERE id='$id'" Since you want to be updating the field, not with a_1 for example, but with the value of $a_1 This is what I was searching for, thank you. However it does not seem to be working for me. The a_1 field is updated now with "$a_1", not with the value of $a_1... Have not found out yet what I should do with this but I'm experimenting at the moment, if any clues please halp. Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted January 28, 2008 Share Posted January 28, 2008 try the following code foreach ($_POST as $key=>$val){ $$key = $val; } $poop=1; while($poop<4) { $dodo="a_".$poop; $sql = "UPDATE abc SET $dodo='".${$dodo}."' WHERE id='$id'" mysql_query($sql,$db) or die(mysql_error()); $poop++; } Quote Link to comment Share on other sites More sharing options...
Badboy8 Posted January 28, 2008 Author Share Posted January 28, 2008 This worked, thanks (very much) Had my head wrapped around this problem the whole weekend... 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.