我在 MySQL 数据库中有一个 SUBJECT 表,我在带有文本框数组的 php 页面中检索其值。同时检索它仅显示文本框中字符串的第一个单词。 例如——主题名称“C 简介”,但在文本框中仅显示“简介”。请参阅引用代码--
<table width="400" border="1">
<tr>
<td colspan="4" valign="top"><div align="center" class="style4">Subject(s) Details</div></td>
</tr>
<tr>
<td width="87"><strong><span class="style1">Select </span></strong></td>
<td width="87"><strong><span class="style1">Subject ID </span></strong></td>
<td width="270"><strong><span class="style1">Subject Name </span></strong></td>
</tr>
<?php
$i=0;
$result = mysql_query("select sub_id,sub_name from subject where crs_id='$cid' and sem_id='$smid' order by sub_id asc")
or
die(mysql_error());
while($row = mysql_fetch_array($result))
{
?>
<tr>
<td><span class="style1"><?php echo "<input name=t1[] type=checkbox value=".$i." checked />"; ?></span></td>
<td><span class="style1"><?php echo "<input name=t2[] size=15 type=text value=".$row['sub_id']." />"; ?></span></td>
<td><span class="style1"><?php echo "<input name=t3[] size=30 type=text value=".$row['sub_name']." />"; ?></span></td>
</tr>
<?php
$i++;
}
?>
<tr>
<td colspan="4"><div align="center"><input name="submit" type="Submit" value="Update Subject(s)" />
</div></td>
</tr>
</table>
最佳答案
如果这是您的实际代码,您需要将数据库值括在单引号内 '
:
<?php echo "<input name=t3[] size=30 type=text value='".$row['sub_name']."' />"; ?>
如果不是,则 html 被解释为:
<input name=t3[] size=30 type=text value=Intro to C />
顺便说一句,这是所有 html 属性的最佳实践:
<?php echo "<input name='t3[]' size='30' type='text' value='".$row['sub_name']."' />"; ?>
结果:
<input name='t3[]' size='30' type='text' value='Intro to C' />
关于php - 文本框数组仅显示数据库中字符串的第一个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23718811/