将值插入另一个表后,如何将值插入另一个表

时间:2017-04-09 02:18:16

标签: php mysql codeigniter

如何插入或更改提交的'参赛者中的字段'表格进入' 1'在我将数据插入' tbl_rate'表??

表' tbl_rate'

---------------------------------
rate_id  judge_id  cont_id  score
---------------------------------
14      | 36     |     5 |  10
---------------------------------

表'参赛者'

id  event_id  name  gender  address  date_created  submitted
------------------------------------------------------------
5     | 25  |john doe |male |Texas |  2017-03-06|   0
------------------------------------------------------------

WHERE 'id' in 'contestants' table is foreign key in 'cont_id' on 'tbl_rate'

2 个答案:

答案 0 :(得分:1)

进行两个单独的查询,一个用于tbl_rate(插入),另一个用于contestants(更新)并按顺序运行它,即tbl_rate,并且在成功运行第二个查询时。

您还可以使用事务来维护数据完整性。

答案 1 :(得分:0)

请尝试以下代码,

$sql = "INSERT INTO tbl_rate (judge_id, cont_id, score) VALUES ('".$judge_id."', '".$cont_id."', '".$score."')";

if ($conn->query($sql) === TRUE) {
  $sql = "UPDATE contestants SET submitted=1 WHERE id=".$cont_id;
  if ($conn->query($sql) === TRUE) {
    echo "Record updated successfully";
  } else {
    echo "Error updating record: " . $conn->error;
  }
 } else {
   echo "Error: " . $sql . "<br>" . $conn->error;
 }

并在查询之前定义变量

相关问题