文本字段中的回声字段值

时间:2012-02-22 15:38:19

标签: php

我想知道是否有人可以帮助我。

通过我之前从这个网站收到的一些指导,我已经能够将下面的脚本放在一起,列出日期信息和相关的单选按钮,允许选择每个记录。

<?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> 

3 个答案:

答案 0 :(得分:0)

您试图在变量被分配之前回显该变量,移动您的while循环以将其包含在内。

答案 1 :(得分: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>

答案 2 :(得分:0)

我想,您应该像 那样循环

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

  // table
}