使用另一个表中的值将动态复选框写入表

时间:2013-01-28 15:55:57

标签: php mysql checkbox

问题的当前摘要:我从表'main'中提取信息,列出特定ID的目标号码和目标名称(最多可以有10行)。它们显示在网页的表格中。我在第一列中添加了反映表中行数的复选框。代码如下,这部分工作正常。输出为3列,复选框,目标编号,目标名称,最多10行,具体取决于该表中特定ID的信息量。

goals.php

<tr>
        <td valign=top colspan=3>
            Current Goals<br>
                <?php
                //this data is being pulled from 'main'
                    $result = mysql_query("SELECT * FROM main WHERE idnumber = $idnumber ORDER BY goalnumber");
                    if ($result) {
                    $total_rows = mysql_numrows($result);
                    } else { $total_rows = 0; }
                    print "<table border=1 width=400>\n";
                    print "<tr> \n";
                    print "<th> Checkbox </th> \n";
                    print "<th> Goal Number </th> \n";
                    print "<th> Goal Name </th> \n";
                    print "</tr> \n";
                    //}
                    $i = 0;
                    while ( $i < $total_rows )
                    {
                    $goalno = intval(mysql_result($result, $i, "goalnumber"));
                    $goalname = mysql_result($result, $i, "goalname");
                    $goal_id = intval(mysql_result($result, $i, "goal_id"));
                    print "<tr> \n";
                    print "<td> <center><input type=\"checkbox\" name=\"goal_id_$i\" value=\"$goalnumber - $goalname\"></td> \n";
                    print "<td> $goalnumber </td> \n";
                    print "<td> $goalname </td> \n";
                    print "</tr> \n";
                    $i = $i + 1;
                    }
                ?>
                </table>
        </td>

一旦用户检查了1-10个这些复选框中的任何地方并提交表单,我需要将所述选择的值传递给db(每个选择应写入db表'updated_goals'中的新字段。在提交时,下面的文件被调用。请注意,我正在将复选框值写入同一个数据库'updated_goals'中的另一个表。我不确定代码在哪里出错,但目标是写入值进入goal_id字段的复选框“$ goalnumber - $ goalname”。

goals_submit.php

// process data in table: updated_goals
$query = "SELECT idnumber from updated_goals where idnumber = {$_POST['idnumber']}";
$result = mysql_query($query);
$total_rows = mysql_numrows($result);

            //writing data to 'updated_goals'

        if (isset($_POST ['goal_id_$i'])) { $goal_id = ReplaceQuotes($_POST['goal_id_$i']); } else { $goal_id = ""; }

    $query = "INSERT INTO updated_goals ( ";
    $query .= "goal_id ";
    $query .= ") VALUES ( ";
    $query .= "'$goal_id' ";
    $query .= ")";

1 个答案:

答案 0 :(得分:1)

可能的解决方案:

更改此行:

if (isset($_POST ['goal_id'][$i])) { $goal_id = ReplaceQuotes($_POST['goal_id'][$i]); } else { $goal_id = ""; }

if (isset($_POST ['goal_id_'.$i])) { $goal_id = ReplaceQuotes($_POST['goal_id_'.$i]); } else { $goal_id = ""; }

或 改变这一行

print "<td> <center><input type=\"checkbox\" name=\"goal_id_$i\" value=\"$goalnumber - $goalname\"></td> \n";

print "<td> <center><input type=\"checkbox\" name=\"goal_id[$i]\" value=\"$goalnumber - $goalname\"></td> \n";

如果数组用于输入名称,例如: inputname [] 它可以通过POST或GET获得$ _POST ['inputname'] [index]或$ _GET ['inputname'] [index]