用php中的值回显url

I'm trying to echo multiple urls from mssql database values. Problem is the url. The url is supposed to pass 3 values to the next page but more often than not, the third variable $ID doesn't make it to the url and the next page will return an Undefined index error. I also use $_GET[] methods for the values. heres the code

    $sql2 ="SELECT UnitsinStock FROM Products Order by ProductID";
$stock=sqlsrv_query($con, $sql2);
$sql = "SELECT ProductName FROM Products ORDER BY ProductID";
$products=sqlsrv_query($con, $sql);
    do{
        while($row=sqlsrv_fetch_array($products, SQLSRV_FETCH_ASSOC) AND $row2=sqlsrv_fetch_array($stock, SQLSRV_FETCH_ASSOC)){
        echo "</br><a href="."Product.php?Units=".implode("",$row2)."&Name=".implode("",$row)."&ID=$ID><pre>".implode("", $row)."</pre></a>";
        $ID+=1;}
    }while(sqlsrv_next_result($stock));

Try to use urlencode() when setting values on the URL. It is likely that character such as &, +, = break your URL variables.

That's all I can recommend without seeing where $ID is defined.

Assuming you're showing us everything, the problem might be that $ID is undefined.

Try this:

$sql2 ="SELECT UnitsinStock FROM Products Order by ProductID";
$stock=sqlsrv_query($con, $sql2);
$sql = "SELECT ProductName FROM Products ORDER BY ProductID";
$products=sqlsrv_query($con, $sql);
$ID = 1;
do{
    while($row=sqlsrv_fetch_array($products, SQLSRV_FETCH_ASSOC) AND $row2=sqlsrv_fetch_array($stock, SQLSRV_FETCH_ASSOC)){
    echo "</br><a href="."Product.php?Units=".implode("",$row2)."&Name=".implode("",$row)."&ID=$ID><pre>".implode("", $row)."</pre></a>";
    $ID+=1;}
}while(sqlsrv_next_result($stock));