dongling3243 2011-03-29 21:20
浏览 19

通过PHP Web表格表更新mySQL表中的多条记录?

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. 
" .
"<ol>";
   while ($i < $n)
{
echo "<hr><P>{$score[$i]} 
";
echo "<hr><P>{$student_id[$i]} 
";

$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!

  • 写回答

1条回答 默认 最新

  • dourao1968 2011-03-29 21:26
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)