php换行导致字符串值消失
我有一些 php 查询和打印医疗招聘网站上 mysql 表的信息。 有时用户输入时会导致换行,而我不想这样,因为 它以不应该的方式影响呈现的 HTML 表格的布局。
当字符串填写表单并将数据写入表中时,我可以修剪字符串,但那里已经有很多数据,所以我决定删除页面上的换行符 php 读取并打印结果的地方。
我声明了一个会导致麻烦的字符串:
$specialtr = trim($special, "\n");
然后渲染 html 表格的位置:
echo '<td width="150">' . $specialtr . '</td>';
这是整个代码:
全部代码有效,只是我添加的上面的部分现在导致带有 $specialtr 变量的列不打印/打印为空。
<form action="selected.php" method="POST">
<table border="1" cellpadding="2" cellspacing="2" style="width: 98%; margin-left: auto; margin-right: auto;">
<tr>
<td>
<?php
$dbhost = 'xxx';
$dbuser = 'xxx';
$dbpass = 'xxx';
$dbname = 'xxx';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die (mysql_error($conn));
mysql_select_db($dbname) or die(mysql_error($conn));
$specialtr = trim($special, "\n");
echo '<table border="1" cellpadding="2" cellspacing="2" style="margin-left: auto; margin-right: auto;">';
$query = 'SELECT userid FROM employee
ORDER BY userid ASC';
$result = mysql_query($query, $conn) or die(mysql_error($conn));
$num_entries = mysql_num_rows($result);
while ($row = mysql_fetch_assoc($result)) {
foreach ($row as $value) {
echo '<tr>';
echo '<td><input type="radio" name="employee" value="' . $row['userid'] . '"/></td>';
echo '<td>Select</td>';
}
}
echo '</tr>';
echo '</table>';
?>
</td>
<td>
<?php
echo '<table border="1" cellpadding="2" cellspacing="2" style="width: 98%; margin-left: auto; margin-right: auto;">';
echo '<tr>';
$query = 'SELECT userid, name, surname, sex, age, nationality, email, telnr, special FROM employee
ORDER BY userid ASC';
$result = mysql_query($query, $conn) or die(mysql_error($conn));
while ($row = mysql_fetch_assoc($result)) {
extract($row);
echo '<td>' . $userid . '</td>';
echo '<td>' . $name . '</td>';
echo '<td>' . $surname . '</td>';
echo '<td>' . $sex . '</td>';
echo '<td>' . $age . '</td>';
echo '<td>' . $nationality . '</td>';
echo '<td>' . $email . '</td>';
echo '<td>' . $telnr . '</td>';
echo '<td width="150">' . $specialtr . '</td>';
echo '</tr>';
}
echo '</table>';
?>
</td>
</tr>
</table>
<br/>
<br/>
<input type="submit" name="go" value="Go!" />
</form>
该代码运行得很好,但由于我添加了修剪,整个列( $special 列消失了)。
我希望也许有人可以告诉我为什么它消失或者我的语法是否有一点错误。
I have some php querying and printing the info of a mysql table on a medical recruitment website.
Sometimes the users enter, causing line breaks where I would rather not have them, because
it affects the layout of the rendered HTML table in a way that it shouldn't.
I can trim the strings when they fill in the forms and the data get written into the table, but there's already a lot of data there, so i decided to remove the line breaks on the page
where php reads and prints the results.
i declared one of the strings where this causes trouble as such:
$specialtr = trim($special, "\n");
and then where the html table gets rendered:
echo '<td width="150">' . $specialtr . '</td>';
Here is the whole code:
All the code works, it's only the parts above that I added that now causes the column with $specialtr variable not to print/print as empty.
<form action="selected.php" method="POST">
<table border="1" cellpadding="2" cellspacing="2" style="width: 98%; margin-left: auto; margin-right: auto;">
<tr>
<td>
<?php
$dbhost = 'xxx';
$dbuser = 'xxx';
$dbpass = 'xxx';
$dbname = 'xxx';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die (mysql_error($conn));
mysql_select_db($dbname) or die(mysql_error($conn));
$specialtr = trim($special, "\n");
echo '<table border="1" cellpadding="2" cellspacing="2" style="margin-left: auto; margin-right: auto;">';
$query = 'SELECT userid FROM employee
ORDER BY userid ASC';
$result = mysql_query($query, $conn) or die(mysql_error($conn));
$num_entries = mysql_num_rows($result);
while ($row = mysql_fetch_assoc($result)) {
foreach ($row as $value) {
echo '<tr>';
echo '<td><input type="radio" name="employee" value="' . $row['userid'] . '"/></td>';
echo '<td>Select</td>';
}
}
echo '</tr>';
echo '</table>';
?>
</td>
<td>
<?php
echo '<table border="1" cellpadding="2" cellspacing="2" style="width: 98%; margin-left: auto; margin-right: auto;">';
echo '<tr>';
$query = 'SELECT userid, name, surname, sex, age, nationality, email, telnr, special FROM employee
ORDER BY userid ASC';
$result = mysql_query($query, $conn) or die(mysql_error($conn));
while ($row = mysql_fetch_assoc($result)) {
extract($row);
echo '<td>' . $userid . '</td>';
echo '<td>' . $name . '</td>';
echo '<td>' . $surname . '</td>';
echo '<td>' . $sex . '</td>';
echo '<td>' . $age . '</td>';
echo '<td>' . $nationality . '</td>';
echo '<td>' . $email . '</td>';
echo '<td>' . $telnr . '</td>';
echo '<td width="150">' . $specialtr . '</td>';
echo '</tr>';
}
echo '</table>';
?>
</td>
</tr>
</table>
<br/>
<br/>
<input type="submit" name="go" value="Go!" />
</form>
The code worked well enough, but since I added the trim, that entire column (the $special column disappears).
I hope maybe someone can tell me why it disappears or if my syntax has a little mistake.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

像这样使用
extract()
是一件可怕的事情。作为一般规则,应避免使用该功能。另请注意,您是在定义 $special 之前执行
trim()
调用,因此您的 $specialtr 变量将是一个空字符串。在开始获取数据库结果并将结果行提取到 $special 之前调用该函数不会神奇地应用trim()
来到达您已获取的行。如果您希望在获取结果时修剪结果,则必须在获取后应用修剪:Using
extract()
like that is a horrible thing. The function should be avoided as a general rule.Also note that you're doing your
trim()
call BEFORE $special is defined, so your $specialtr variable is going to be an empty string. Calling the function before you start fetching your database results and extracting a result row into $special will not magically applytrim()
to reach row you've fetched. If you want the results to be trimmed as you fetch them, then you have to apply the trim AFTER you do the fetch: