0

在我的表单上,我有一个回显链接到我的数据库中的数据。但是,如果这是第一次访问表单(表中没有与会话相关的 ID),则文本框不会出现,而它们应该出现但为空!当id链接到会话并且数据库中有一行时成功(用户已经完成了一次表单并返回编辑。)我希望这是有道理的。为了更清楚一点,如果用户第一次访问表单,文本区域应该在那里(它们现在消失)但是是空的。

 $rs_settings = mysql_query("SELECT * from thesis WHERE user_id = $user_id;");

     <br>

  <form action="thesis.php" method="post" name="regForm" id="regForm" >

  <?php while ($row_settings = mysql_fetch_array($rs_settings)) {?>

      Title of Proposed Thesis<span class="required">*</span>

      <textarea name="thesis_Name" type="text" style="width:500px; height:150px" 

      id="thesis_Name" size="600"><?php echo $row_settings['thesis_Name']; ?>   
      </textarea>
4

2 回答 2

1

使用mysql_num_rows()


<form action="thesis.php" method="post" name="regForm" id="regForm" >
<?php
$num_rows = mysql_num_rows($rs_settings);
if($num_rows > 0) { ?>
<?php while ($row_settings = mysql_fetch_array($rs_settings)) {?>

//your textarea with values from database

<?php } 
} else { ?>
//empty textares
<textarea></textarea>

<?php } ?>

你的意思是这样的吗

于 2012-04-27T08:23:31.063 回答
0

请尝试下面给出的代码,它可能会有所帮助..

<form action="thesis.php" method="post" name="regForm" id="regForm" >

    <?php if (mysql_num_rows($rs_settings) <= 0) { ?>
        <textarea name = "thesis_Name" type = "text" style = "width:500px; height:150px"

                id = "thesis_Name" size = "600"></textarea>
                <?php
            } else {
                while ($row_settings = mysql_fetch_array($rs_settings)) {
                    ?>

            Title of Proposed Thesis<span class="required">*</span>

            <textarea name="thesis_Name" type="text" style="width:500px; height:150px" 

                    id="thesis_Name" size="600"><?php echo $row_settings['thesis_Name']; ?>   
            </textarea>
            <?php
        }
    }?>

这里mysql_num_rows($query)首先检查结果是否存在任何行

但如果表单有多个字段,那么

<?php
$rs_settings = mysql_query("SELECT * from thesis WHERE user_id = {$user_id}") or die(mysql_error());
$result = array();
if (mysql_num_rows($rs_settings) == 0) {
    $result[0]['thesis_Name'] = "";
    $result[0]['field1'] = "";
} else {
    $i = 0;
    while ($res = mysql_fetch_array($rs_settings)) {
        $result[$i] = $res;
        $i++;
    }
}
foreach ($result AS $index => $r) {
    ?>

    Title of Proposed Thesis<span class="required">*</span>

    <textarea name="thesis_Name" type="text" style="width:500px; height:150px" 

            id="thesis_Name" size="600"><?php echo $r['thesis_Name']; ?>   
    </textarea>
<?php } ?>
于 2012-04-27T10:36:02.873 回答