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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.