douyun3887 2012-02-20 08:24
浏览 61
已采纳

获取$ row的值并记录到另一个表

i have a table and it display all the record in the table using $row. It generates multiple lines because it displayed all the value from the table. Then inside the table i created a form that comprised of only one single row. I want to record one value of that single $row using the studentnumber and subjectcode which is both from the $row in my query. Sorry for my confusing question and THANKS in advance.

code:

  echo "<form name=\"grades\"  action=\"insert.php\" method=\"post\" onsubmit=\"javascript:return confirm('Confirm Grade Submission?');\">";

  echo "<td class=\"studentlist\">" . $row['studentnumber'] . "</td>";
  echo "<td class=\"studentlist\">" . $row['subject'] . "</td>";
  echo "<td class=\"studentlist\"><input type=\"text\" id= \"q1\" name=\"q1\" class=\"tbox\"></td>";
  echo "<td class=\"studentlist\"><input type=\"text\" id= \"q2\" name=\"q2\" class=\"tbox\"></td>";
  echo "<td class=\"studentlist\"><input type=\"text\" id= \"q3\" name=\"q3\" class=\"tbox\"></td>";

  echo "<td class=\"studentlist\"><input type=\"submit\" value=\"Submit\"></td>";
  echo "</form>";

insert.php

$studentnumber= $_POST['studentnumber'];
$subjectcode= $_POST['subjectcode'];

 $query = ("SELECT * FROM grades WHERE studentnumber = '$studentnumber' && subjectcode = '$subjectcode'");
 $result=mysql_query($query);

if($result)
  {
    if(mysql_num_rows($result) == 1)

      {
      $sql="INSERT INTO grades ( q1, q2, q3 )
        VALUES('$_POST[q1]','$_POST[q2]','$_POST[q3]'')";

            if (!mysql_query($sql,$con))
              {
                die('Error: ' . mysql_error());
              }
              echo "<script type='text/javascript'>alert('Grade Successfully Submitted');location.href = '../administrator/admin_home.php';</script>";
      }
  }
  • 写回答

1条回答 默认 最新

  • dongsheng1238 2012-02-20 08:49
    关注

    First things first, you will need to pass the studentnumber and subjectcode from the form to the second page which processes the MySQL. For this you can use some hidden form fields, i.e. use in place of the lines where you echo studetnumber and subject.

    echo "<td class=\"studentlist\"><input type=\"hidden\" name=\"studentnumber\" value=\" . $row['studentnumber'] . \">" . $row['studentnumber'] . "</td>";
    echo "<td class=\"studentlist\"><input type=\"hidden\" name=\"subject\" value=\" . $row['subject'] . \">" . $row['subject'] . "</td>";
    

    It's hard to be specific without seeing the full table design of grades, however the following would insert the data you are trying to insert however it would error if studentnumber and subjectcode formed a UNIQUE key, and the row already existed

    $sql = "INSERT INTO grades (
        studentnumber,
        subjectcode,
        q1,
        q2,
        q3
    ) VALUES (
        '".$_POST['studentnumber']."',
        '".$_POST['subject']."',
        '".$_POST['q1']."',
        '".$_POST['q2']."',
        '".$_POST['q3']."'
    )";
    

    In order, if it hits a duplicate studentnumber/studentcode combination, to make it update then you just add "ON DUPLICATE KEY" syntax to the end, i.e.

    $sql = "INSERT INTO grades (
        studentnumber,
        subjectcode,
        q1,
        q2,
        q3
    ) VALUES (
        '".$_POST['studentnumber']."',
        '".$_POST['subject']."',
        '".$_POST['q1']."',
        '".$_POST['q2']."',
        '".$_POST['q3']."'
    )
    ON DUPLICATE KEY UPDATE
        q1 = VALUES(q1),
        q2 = VALUES(q2),
        q3 = VALUES(q3)";
    

    This will then update q1,q2,q3 with whatever values were specified for the row(s) that had duplicate student number/subject combinations.

    Finally if you just want to ignore duplicates, then the following will suffice (note addition of IGNORE).

    $sql = "INSERT IGNORE INTO grades (
        studentnumber,
        subjectcode,
        q1,
        q2,
        q3
    ) VALUES (
        '".$_POST['studentnumber']."',
        '".$_POST['subject']."',
        '".$_POST['q1']."',
        '".$_POST['q2']."',
        '".$_POST['q3']."'
    )";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看