gpt4 book ai didi

php - 从数据库填充 PHP 数组以创建动态 HTML 表单

转载 作者:行者123 更新时间:2023-11-29 08:18:50 27 4
gpt4 key购买 nike

我们希望使用数组动态创建一个包含数据库内容的表单。从数据库中提取数据很容易:

$sql_inhalt = "SELECT * FROM tbl_inhalt ORDER BY id ASC";
$result_inhalt = mysql_query($sql_inhalt) or die("INHALT".mysql_error());
while($rs_inhalt = mysql_fetch_row($result_inhalt)){
$id[] = $rs_inhalt[0];
$text[] = $rs_inhalt[2];
}

但是我们如何将这些数据应用到表单中,以便 $id 和 $text 相对应?:

<form id="form1" name="form1" method="post" action="..." >
<?php foreach($text as $out_text){
echo '<textarea name="'.$id.'" type="text" class="mceEditor" cols="85" rows="5" />'.$out_text.'</textarea>';
}?>
</form>

非常感谢您的帮助!

最佳答案

只需使用一个数组即可实现两者。

while ($rs_inhalt=...) {
$data[] = array($rs_inhalt[0], $rs_inhalt[2]);
}

在你看来

foreach ($data as $pair) {
// acces id as $pair[0] and text as $pair[1]
}

另请记住,mysql_* 函数为 officially deprecated因此不应在新代码中使用。您可以使用 PDO 或 MySQLi 代替。请参阅this answer in SO了解更多信息。

关于php - 从数据库填充 PHP 数组以创建动态 HTML 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20005916/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com