concatenating two variable into one [closed]
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}."'";
}
}
You missed _ in
${"ordernum".$x} = $_POST["ordernum".$x.""];
here --^
Just $ordernum.$x is all that is required to append one var to another
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
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).
精彩评论