2012-02-22 54 views
1

我想知道是否有人能夠幫助我。文本字段中的回聲字段值

通過我之前從本網站收到的一些指導,我已經能夠將腳本放在一起,下面列出了日期信息和相關的單選按鈕,允許選擇每個記錄。

<?php 

mysql_connect("hostname", "username", "password")or 
die(mysql_error()); 
mysql_select_db("database"); 
?> 
<html><head> 
<title>Location</title> 
<style type="text/css"> 
<!-- 
.style3 { 
    font-size: 18px; 
    font-family: Calibri; 
} 
--> 
</style> 
</head> 
<form name="addimages"> 
    <p> 
    <label> 
    <div align="center" class="style3"> Location Name <br> 
     <br> 
     <input name="locationname" type="text" value="<?php echo $locationname;?>" id="locationname" size="80"> 
    </div> 
    </label> 
    </p> 
    <p>&nbsp;</p> 
    <p> 
    <?php 

$result = mysql_query("SELECT userdetails.userid, finds.dateoftrip, finds.userid, finds.locationid, detectinglocations.locationid, detectinglocations.locationname, finds.findname, finds.finddescription FROM userdetails, finds, detectinglocations WHERE finds.userid=userdetails.userid AND finds.locationid=detectinglocations.locationid AND finds.userid = 1 ORDER BY dateoftrip DESC"); 
if (mysql_num_rows($result) == 0) 
// table is empty 
    echo 'There are currently no finds recorded for this location.'; 
else 
{ 

    while (list($userid, $dateoftrip, $locationname) = 
    mysql_fetch_row($result)) 

    { 
     echo"<table>\n"; 
     echo"<tr>\n" 
    . 
    "<td><input type='radio' name='radio' dateoftrip value='{$userid}' /></td>\n" 
    ."<td><small>{$dateoftrip}</small><td>\n" 
    ."</tr>\n"; 
    } 
    echo"<tr><td colspan=\"3\">"; 
    echo"<br>"; 
    echo"</td></tr>"; 
    echo'</table>'; 
} 
?> 
</p> 
</form> 

我現在試圖做的是將'locationname'值添加到適當的文本字段。

我已將value="<?php echo $locationname;?>"添加到文本字段,但它仍爲空白。

我認爲問題就出在這裏:

while (list($userid, $dateoftrip, $locationname) = mysql_fetch_row($result))我用得到的日期列表中的信息,而通常我會用這樣的:
while ($row = mysql_fetch_array($result)) { $locationname = $row['locationname'];獲取的文本字段的值。

有人可能告訴我,請問是否有一種方法可以將兩條語句合併爲一個我需要的信息,即包含日期列表的表格,同時將'locationname'值'回顯'到文本域。

非常感謝

更新的代碼

<?php 
mysql_connect("hostname", "username", "password")or 
die(mysql_error()); 
mysql_select_db("database"); 

$locationname=''; 
$result = mysql_query("SELECT userdetails.userid, finds.dateoftrip, finds.userid, finds.locationid, detectinglocations.locationid, detectinglocations.locationname, finds.findname, finds.finddescription FROM userdetails, finds, detectinglocations WHERE finds.userid=userdetails.userid AND finds.locationid=detectinglocations.locationid AND finds.userid = 1 ORDER BY dateoftrip DESC"); 
if (mysql_num_rows($result) == 0){ 
// table is empty 
    $return = 'There are currently no finds recorded for this location.'; 
    }else{  
    $return="<table>\n";  
    while (list($userid, $dateoftrip, $locationname) = mysql_fetch_row($result)){ 

$return .="<tr>\n". 
"<td><input type='radio' name='radio' dateoftrip value='{$userid}' /></td>\n" 
."<td><small>{$dateoftrip}</small><td>\n" 
."</tr>\n"; 
} 
$return .="<tr><td colspan=\"3\"><br>"; 
} 
?> 
<form name="addfinds"> 

     <input name="locationname" type="text" value="<?php echo $locationname;?>" id="locationname" size="80"> 
    <p>&nbsp;</p> 
    <p> 
    <?php echo $return;?> 
</p> 
</form> 

回答

0

您試圖呼應變量它甚至分配之前,請將您的while循環,包括它裏面。

0

基本上你試圖在設置它之前使用它,如果你有error_reporting你會看到錯誤。你所需要的是,你不應該在while循環中使用回聲,你應該建立輸出並將代碼移到輸入文本的上方。事情是這樣的:

<?php 
... 
$locationname=''; 
$result = mysql_query("SELECT ..."); 
if (mysql_num_rows($result) == 0){ 
    $return = 'There are currently no finds recorded for this location.'; 
}else{ 
    $return="<table>\n"; 
    while (list($userid, $dateoftrip, $locationname) = mysql_fetch_row($result)){ 

     $return .="<tr>\n". 
     "<td><input type='radio' name='radio' dateoftrip value='{$userid}' /></td>\n" 
     ."<td><small>{$dateoftrip}</small><td>\n" 
     ."</tr>\n"; 
    } 
    $return .="<tr><td colspan=\"3\"><br>"; 
    ... 
    ... 
} 

?> 
<html><head> 
<title>Location</title> 
... 
... 
     <input name="locationname" type="text" value="<?php echo $locationname;?>" id="locationname" size="80"> 
... 
    <p>&nbsp;</p> 
    <p> 
    <?php echo $return;?> 
</p> 
</form> 
+0

你好,非常感謝這一點。我已經修改了我的代碼以包含您的建議並更新了我的原始帖子。但不幸的是,雖然查詢運行,但'locationname'值不填充文本字段。請你可以告訴我哪裏出了問題? – IRHM 2012-02-22 16:15:13

+0

我可以onnly建議你不要使用'list($ userid,$ dateoftrip,$ locationname)'approch並只使用'$ row = mysql_fetch_assoc($ result)'然後存儲'$ locationname = $ row ['locationname']'當通過循環時 – 2012-02-22 16:20:50

0

我想,你應該改變循環是這樣的:

while($row = mysql_fetch_row($result)) { 
    list($userid, $dateoftrip, $locationname) = $row; 

    // table 
}