2012-07-14 60 views
1

我試圖使用$_GET[]挑ID,並將其分配給一個變量但是它不工作。當我嘗試直接回顯$_GET[]時,它可以很好地在頁面上顯示ID,但是當我將它分配給一個變量並嘗試回顯它時,它將無法正常工作。例如,這不會工作:

$sel_hotel = $_GET[]; 
echo $sel_hotel; 

的代碼看起來很好有一個與它沒有任何問題,但它只是不說值傳遞給變量i相信有可能是壞了我的php.ini文件,但我不確定。我正在使用PHP版本5.4.3。請幫忙 。非常感謝您

<?php 
if(isset($_GET['hotl'])){ 
$sel_hotel = $_GET['hotl']; 
$sel_hotel =""; 
echo $sel_hotel; 
}elseif(isset($_GET['room'])){ 
$sel_room = $_GET['room']; 
$sel_room =""; 
echo $sel_room; 
}else{ 
$sel_hotel =""; 
$sel_room =""; 
} 
echo $sel_hotel; 

?> 
<?php require_once("includes/header.php");?> 
<?php require_once("includes/function.php");?> 
<?php //require_once("TheDatabase.php")?> 
<?php $connection = mysql_connect("localhost","root","root"); 
     if(!$connection){ 
     die("Database Connection Failed :". mysql_error()); 
     }else{        
       $db_select = mysql_select_db("travelnstay", $connection); 
       if(!$db_select){ 
       die("Database Selection Failed:". mysql_error()); 
       } 
     } 
?> 
<div class="Calign"> 
<div class="mar"> 
<div> 
<p>Menu</p> 
<?php 
       $hotel_set = select_all_hotels(); 
       while($hotel = mysql_fetch_array($hotel_set)){ 
        echo "<p class=\"mar\"><a href=\"admincontent.php?hotl=" . 

urlencode($hotel["hotel_id"]). 

        "\">{$hotel["hotel_name"]}</a></span></p>"; 
        $room_set = room_by_id($hotel["hotel_id"]); 
        echo "<ul>"; 
        while($room= mysql_fetch_array($room_set)){ 
        echo "<li><a href=\"admincontent.php?room=". urlencode($room["room_id"]). 
        "\">{$room["room_type"]}</a></li>";     
        echo"</ul>"; 
        } 
       } 


    echo "<p> Its is suppose to be here".$sel_hotel."</p>"; 
    echo "<p>". $sel_room. "</p>"; 


    ?> 


</div><!--end of the mar--> 
</div><!--end of the Calign--> 
</body> 

</html> 

回答

3

您從$_GET設置變量每一次,你後刪除它們......

嘗試:

<?php 
    $sel_hotel = ""; 
    $sel_room = ""; 

    if(isset($_GET['hotl'])) 
    { 
    $sel_hotel = $_GET['hotl']; 
    echo $sel_hotel; 
    } 
    elseif(isset($_GET['room'])) 
    { 
    $sel_room = $_GET['room']; 
    echo $sel_room; 
    } 

    echo $sel_hotel; 
?> 
+0

你的代碼是完美的 – 2012-07-14 17:07:35

+0

Dude你是曼 – 2012-07-14 17:08:24

+0

我還有一件事,如果它不是太多問,我使用PHP版本5.4.3與MAMP但錯誤不會顯示在頁面上。當出現錯誤時頁面變爲空白。我不知道在php.ini中的配置。 – 2012-07-14 17:11:19

1

您檢索$ _GET復位後的值的變量在這兩種情況下 - 它們都是空的,因爲你正在清空它們。

1

的問題是,一旦您設置的變量,你清楚他們是一個空字符串。第二行是這個問題:

$sel_hotel = $_GET['hotl']; 
$sel_hotel =""; 

你試圖用這個邏輯流程實現什麼?我建議或者只是刪除這些行或將它們移動到腳本的頂部,如果在運行時需要確保它們是空的。