开发者

Updating multiple records in mySQL table via PHP web form table?

Need a little help...

I have a basic html table with text field form in last column and a hidden field on each row of the web table. The data in this table is extracted out of a table in the database.

I want my users to be able to update one field in the database (a score) using this web form (page).

I have a hidden web form component on each row that contains the unique id of the record in the database for each row in the web page table.

I was attempting to create code that would update the entire list of entries on the web form, even if the user is not updating that particular field. (The values of the scores field are populated into the web form at the creation of the table. So if you did not update any scores, but hit the submit button, it would update the database table with the same values.)

Here’s my code: (abbreviated to save bytes…)

<?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");开发者_开发问答
    }
?>

Am I on the right track? Is there a better way to do what I’m attempting? All suggestions and ideas welcome!

Thank you in advance!


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜