doufei2355 2013-04-29 14:35
浏览 10

如何连续创建提交表单并将输入与该行相关联?

Hey Guys, I have a question for you.

Imagine that I wanted to be able to keep track of how many miles I've ran every week, so that I could compare it to the goals I've set for each week. So i've created this table by the use of mysql_fetch_row.

$result=mysql_query("SELECT * FROM randomtable ORDER BY week ASC");

echo "<Table id='result' cellspacing='0'>
    <tr class='toprow'>
    <th>Week</th>
    <th>Goal</th>
    <th>Actual Number of Miles</th>
    </tr>";

while($row = mysql_fetch_row($result))
{
    echo "<tr class='standardrow'>";
    echo "<td>$row[0]</td>";
    echo "<td>$row[1]</td>";
    echo "<td><form><input method='post' type='number'></form></td>";
    echo "</tr>";
}

echo "</table>";

This piece of code resultet in a table with 10 weeks with 10 goals - and a column for the actual number of miles. This column should include 10 input forms where the actual number of miles can be submitted. But how do I relate the input from the submit form to the row in which the submit form is positioned? The primary key is the week - so this would be the one to relate to.

Hope you understand what my problem is:)

  • 写回答

3条回答 默认 最新

  • duandaoji3992 2013-04-29 14:38
    关注

    To do this you would use a hidden input field.

    When you echo each row, and the form in that row, you would simply add an extra line:

    `<input type="hidden" name="row_id" value="' . $row['id_column'] . '" />';
    

    In full, your code would be:

    $result=mysql_query("SELECT * FROM randomtable ORDER BY week ASC");
    
    echo "<Table id='result' cellspacing='0'>
        <tr class='toprow'>
        <th>Week</th>
        <th>Goal</th>
        <th>Actual Number of Miles</th>
        </tr>";
    
    while($row = mysql_fetch_row($result))
    {
        echo "<tr class='standardrow'>";
        echo "<td>$row[0]</td>";
        echo "<td>$row[1]</td>";
        echo "<td>
                <form>
                    <input method='post' type='number'>
                    <input type='hidden' name='row_id' value='" . $row['id_column'] . "' />
                </form>
              </td>";
        echo "</tr>";
    }
    
    echo "</table>";
    
    评论

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决