tags:

views:

69

answers:

4

I'm new here and kind pretty new to PHP and I'm no sure as to why this is not working.

If I echo $ordernum1, I get the value I look for, but echoing echo ${"ordernum".$x}; gives me nothing.

I have also echoed $attempts and I get the value I'm looking for. Any help would be great thank you

$update=$_POST['update']; //echo $update;
$attempts=$_POST['attempts'];//echo $attempts;

if($update==2){
    for($x=0; $x<=$attempts; $x++){
        ${"ordernum".$x} = $POST["ordernum".$x.""]; echo ${"ordernum".$x};
        $query="UPDATE OrderTrack
        SET applicationID='--junk($appid)'
        WHERE OrderNum='".${"ordernum".$x}."'";
    }
}
+1  A: 

Just $ordernum.$x is all that is required to append one var to another

DBQ
+5  A: 

You missed _ in

${"ordernum".$x} = $_POST["ordernum".$x.""];
             here --^
M42
+1  A: 

Try

$update = $_POST['update']; //echo $update; $attempts=$_POST['attempts'];//echo $attempts;

if ($update == 2) {
  for ($x = 0; $x <= $attempts; $x++) {
    $ordernum = $_POST["ordernum" . $x];
    echo $ordernum;
    $query = "UPDATE OrderTrack SET applicationID='--junk($appid)' WHERE OrderNum = '$ordernum'";
  }
}

You dont need to set a new variable in the loop, you can just reuse the $ordernum

Hope this helps

Luke

Luke
A: 

I wouldn't pollute your local scope with tons of related variables like this... Create an array to store that info

$ordernums = array();
if($update==2){
    for($x=0; $x<=$attempts; $x++){
        $ordernums[$x] = $_POST["ordernum".$x.""];

Not to mention that you have a massive SQL injection vulnerability in there. You need to escape any varaibles using something like mysql_real_escape_string() or mysqli::real_escape_string(). Or use a parameterized query (which is the best alternative).

ircmaxell
your array idea it looks and seem really clean, but to be honest i have no idea whay a massive SQL injection vulnerability im still pretty new to both SQL and PHP
mike
does array() call all my $_POST['ordernums'] or does it call every $_POST?
mike
so i implemented the array() and it speed my code up tremendously thanks alot!
mike
The SQL injection vulnerability is because you're putting raw inputted data into a query. So if someone entered `';DROP TABLE OrderTrack; --` for `ordernum1`, the query would be `UPDATE OrderTrack SET ... WHERE OrderNum = ''; DROP TABLE OrderTrack; --';`... That would be very bad. So escape the string (using a function like what I posted) before adding it to the query...
ircmaxell
ok cool thanks man i actually noticed this after alot of googling all over the place on our internal tools which and that server isnt connected to the internet so the higher up said not to worry about it
mike
ALWAYS ALWAYS ALWAYS worry about it. Never get into the habit of writing or allowing insecure code. It's a bad idea all around, no matter if it's connected to the internet or not...
ircmaxell