0

少し助けが必要です...

最後の列にテキスト フィールド フォームがあり、Web テーブルの各行に非表示フィールドがある基本的な html テーブルがあります。このテーブルのデータは、データベース内のテーブルから抽出されます。

ユーザーがこの Web フォーム (ページ) を使用して、データベース内の 1 つのフィールド (スコア) を更新できるようにしたいと考えています。

Web ページ テーブルの各行のデータベース内のレコードの一意の ID を含む、各行に非表示の Web フォーム コンポーネントがあります。

ユーザーが特定のフィールドを更新していなくても、Web フォームのエントリのリスト全体を更新するコードを作成しようとしていました。(スコア フィールドの値は、テーブルの作成時に Web フォームに取り込まれます。そのため、スコアを更新せずに送信ボタンを押すと、データベース テーブルが同じ値で更新されます。)

これが私のコードです:(バイトを節約するために省略されています…)

<?php

//Do all the database connection stuff up front


if (isset($_POST[‘score’]))
{
     $student_id = $_POST[‘student_id’];
      $score = $_POST['score'];
      $n        = count($score);
       $i        = 0;

echo "You have updated these student scores on this assignment. \r\n" .
"<ol>";
   while ($i < $n)
{
echo "<hr><P>{$score[$i]} \r\n";
echo "<hr><P>{$student_id[$i]} \r\n";

$qry = "UPDATE assignments SET score = ".$score[$i]." WHERE student_id = " .$student_id[$i]. '"';
$result=@mysql_query($qry);

$i++;
   }
}
if($result) {
        header("location: member-index.php");
        exit();
    }else {
        die("Query failed");
    }
?>

私は正しい軌道に乗っていますか?私が試みていることを行うためのより良い方法はありますか? すべての提案やアイデアを歓迎します!

前もって感謝します!

4

1 に答える 1

0

i'm guessing you are using

<input name="scores[]"/>

why not echo the unique id into the input name?

<input name="score[<?php echo $unique_id ?>]" />

this means in the loop, the $i would be the unique id (one less variable and less HTML).

and please use mysql_real_escape_string() when working with DB transactions. I know it's an example code but please don't forget that

Besides that, yes, you are on the right track

于 2011-03-29T21:26:57.230 に答える