我正在一个名为notesUpdate
的列中将表单数据插入Mysql数据库。下面是我的php脚本,它将数据插入/更新到数据库中,但我使用分隔符___
,因为在这些列中,我插入了许多数据。我想逐行显示所有的数据,以___
结尾。所以我使用第二个Php脚本。
插入/更新php脚本
$contetn = $_POST['contentText'];
$cdid = $_POST['cdid'];
$contetn .= "___";
$query = mysql_query("UPDATE contact_details SET notesUpdate = CONCAT(notesUpdate, '$contetn') WHERE cdid = '$cdid' LIMIT 1");
显示Php脚本
$query = mysql_query("SELECT notesUpdate FROM contact_details WHERE cdid = '$id'");
$row = mysql_fetch_array($query);
$notes = mysql_real_escape_string(htmlspecialchars(trim($row['notesUpdate'])));
$ex = explode("___", $notes);
$ex[0];
$ex[1];
显示数据应如下所示:
Hello one data
Hello two data
Hello three data
但我不知道怎么才能得到这个?你能建议我还是告诉我怎么才能得到这个?谢谢。
更新:
这是所有数据显示的表单:
<table width="500" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><h2>All Notes</h2></td>
</tr>
<tr>
<td><input type="text" name="cdid" value="<?php echo $id; ?>" id="cdid"/></td>
</tr>
<tr>
<td><textarea cols="65" rows="5" name="notesContent" style="padding:0; margin:0;"> <?php echo $ex[0]; ?> </textarea> </td>
</tr>
<tr><td> </td></tr>
<tr>
<td><input type="submit" value="Edit Notes" id="editNotes" class="submit" /></td>
</tr>
</table>
最佳答案
在对注释进行了一些讨论之后,您在for
标记中的foreach
数组中只需要一个ex
或textarea
。但有一个陷阱。如果您只是在数组中迭代,它将只显示一行接一行,因为您没有添加enter
字符,所以它将如下所示:
<textarea cols="65" rows="5" name="notesContent" style="padding:0; margin:0;"><?php foreach( $ex as $value ) { echo $value . "\r\n"; } ?></textarea>
注意:它必须是内联代码,因为HTML标记
textarea
将添加您放入其中的每个字符。如果您想要更漂亮的代码,请这样使用:
<?php
$text = "";
foreach( $ex as $value ){
$text .= $value . "\r\n";
}
?>
<textarea cols="65" rows="5" name="notesContent" style="padding:0; margin:0;"><?php echo $text; ?></textarea>
或者更简单
<textarea cols="65" rows="5" name="notesContent" style="padding:0; margin:0;"><?php echo str_replace( "___", "\r\n", $notes ); ?></textarea>
关于php - 如何使用分隔符逐行显示Mysql数据库中的数据?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24871900/