Jump to content

[SOLVED] making a loop that actually works.


Badboy8

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/88098-solved-making-a-loop-that-actually-works/
Share on other sites

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));
?>

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.

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

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

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.

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++;
}

Archived

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

×
×
  • Create New...

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.