0

我想在 foreach 语句中更新我的数据库,但它给了我插入到我输入的最后一个值的相同值。

假设我在上一页有一个循环文本框。

    $i = 0;
    while($row_recordset = mysql_fetch_array($query_run)) 

            {           
        echo "<tr>";
        <td'><input type='text' name='atten_ave".$i."''></td>
        echo "</tr>";

  $i++;
}

现在此代码将获取文本框的每个值,并且应该从上一页更新数据库。

foreach($_POST as $textbox => $values) {

            $sessionbatch = getbatchno('BATCHNO');

            $query_update = "UPDATE `grades` 
                            SET 
                            `ATTEN_SUM` = '$values'

                            WHERE
                            `BATCHNO` = '$sessionbatch'
                             ";

            if(mysql_query($query_update)){
                echo 'SUCCESS';
                } else{
                        die(mysql_error());
                          }

当我检查我的 ATTEN_SUM COLUMN 时,值与文本框上的最后一个输入相同。

4

1 回答 1

0

First off, do not loop through every value in $_POST. Give your textboxes an idenitcal name that has [] appended to it, like name="atten_ave[]". This will create an array for them within $_POST, then you can loop through them all safely with:

foreach($_POST["atten_ave"] as $textbox => $values) { ....

Now whenever getbatchno('BATCHNO'); returns a value it has already returned within the life of that loop, it will overwrite the update it previously did with that similar value. So if it is returning the same value in every iteration, your query will just keep overwriting itself.

于 2013-04-26T16:49:04.653 回答