需要帮助以增量形式增加 2 个变量
我试图允许用户从显示的表单中使用 POST 编辑一行数据。查询正在运行,并且表格正确显示除表单输入字段中的名称值之外的所有内容。我尝试了多种变体,但名称值始终显示为空白。问题可能出在这一行:
echo $field_name;
这是代码:
<form action="process.php" method="POST">
<?
$qry = "SELECT activity, site, date, FROM home WHERE user_id='$session->user_id' ORDER BY date";
$res = mysql_query($qry);
$field_name = mysql_field_name($res, 0);
function mysql_fetch_all($res) {
while($row=mysql_fetch_array($res)) {
$return[] = $row;
}
return $return;
}
function create_table($dataArr) {
echo "<form action=\"process.php\" method=\"POST\"><table><tr>";
for($j = 0; $j < 3; $j++) {
echo "<td><input type=\"text\" name=\"";
echo $field_name;
echo "\" maxlength=\"30\" value=" .$dataArr[$j]. "></td>";
}
echo "<td><input type=\"hidden\" name=\"subedit\" value=\"1\"><input type=\"submit\" value=\"Update\"></td></tr></table></form>";
}
$all = mysql_fetch_all($res);
echo "<table class='data_table'>";
echo "<tr><td colspan=\"3\"><h2>Current Profile</h2></td></tr>";
echo "<tr><small><td>Activity </td><td>Site </td><td>Date </td></small></tr>";
for($i = 0; $i < count($all); $i++) {
create_table($all[$i]);
}
echo "</table></form>";
I'm trying to allow a user to edit a row of their data with POST from a displayed form. The query is working and the table properly displays everything except a name value in the form input field. I've tried numerous variations but the name value keeps coming up blank. The problem might be with this line:
echo $field_name;
Here is the code:
<form action="process.php" method="POST">
<?
$qry = "SELECT activity, site, date, FROM home WHERE user_id='$session->user_id' ORDER BY date";
$res = mysql_query($qry);
$field_name = mysql_field_name($res, 0);
function mysql_fetch_all($res) {
while($row=mysql_fetch_array($res)) {
$return[] = $row;
}
return $return;
}
function create_table($dataArr) {
echo "<form action=\"process.php\" method=\"POST\"><table><tr>";
for($j = 0; $j < 3; $j++) {
echo "<td><input type=\"text\" name=\"";
echo $field_name;
echo "\" maxlength=\"30\" value=" .$dataArr[$j]. "></td>";
}
echo "<td><input type=\"hidden\" name=\"subedit\" value=\"1\"><input type=\"submit\" value=\"Update\"></td></tr></table></form>";
}
$all = mysql_fetch_all($res);
echo "<table class='data_table'>";
echo "<tr><td colspan=\"3\"><h2>Current Profile</h2></td></tr>";
echo "<tr><small><td>Activity </td><td>Site </td><td>Date </td></small></tr>";
for($i = 0; $i < count($all); $i++) {
create_table($all[$i]);
}
echo "</table></form>";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
$field_name
超出范围(查看 PHP 的变量范围 页面)。尝试更改您的create_table
函数以接受$field_name
var,如下所示:或 使用全局(不推荐)
$field_name
is out of scope (check out PHP's variable scope page). Try changing yourcreate_table
function to accept the$field_name
var like so:Or using global (not as recommended)
尝试以下代码:
但是,我确实建议您尝试创建一个 Table 类。这会更加高效和整洁。
Try this code:
However, I do recommend that you try to create a Table class. It would be much more efficient and neater.