PHP:将数据插入另一个表

时间:2012-09-20 05:09:08

标签: php

我有下面的函数告诉数据库天气更新或插入依赖于是否已找到电子邮件地址这工作正常然而我仍然有一些$POST信息需要进入另一个表依赖于id

我已经知道,对于if($emailCheck != FALSE)语句,我可以运行另一个insert语句并在其中设置id

我的问题是如何才能为insert声明中的新else信息实现这一目标?

插入功能

function Insert()
{
  $emailCheck = $this->checkEmail($email);

  if($emailCheck != FALSE)
  {
    updateQuery by $emailCheck['id'])); // The email address is found so we update the data

  }else{

    Jojo::insertQuery; // The email address is not found so we insert the new row
  }
}

检查功能:

function checkEmail($email)
{
     $email = $POST['Email'];

    if($email)
    {
        $candemail = selectRow("SELECT * FROM {table} WHERE email=?",$email);

        if(isset($candemail['email']))
        {
            return $candemail;
        } else {
            return FALSE;
        }
    }
}

完整功能:

  function candidateInsert($fields, $candiatefilename,$jobid)
    {
      $emailCheck = Jojo_Plugin_name_jobs_apply::checkCandidateEmail($email);

      if($emailCheck != FALSE)
      {
        //Jojo::updateQuery("UPDATE {db} SET first_name=?,last_name=?,home_phone=?,work_phone=?,mobile_phone=?,email=?,skype=?,contact_method=?,location=?,location_other=?,cv_path=? WHERE id=?", array($fields['FirstName'],$fields['LastName'],$fields['HomePhone'],$fields['WorkPhone'],$fields['MobilePhone'],$fields['Email'],$fields['Skype'],$fields['ContactMethod'],$fields['Location'],$fields['LocationOther'],$candiatefilename, $emailCheck['id']));
        //Jojo::insertQuery("INSERT INTO {refocus_candidate_job} SET candidateID=?,jobID=?,appliedDate=?", array($emailCheck['id'],$jobid,date("d/m/y")));
    }else{
        Jojo::insertQuery("INSERT INTO {db} SET first_name=?,last_name=?,home_phone=?,work_phone=?,mobile_phone=?,email=?,skype=?,contact_method=?,location=?,location_other=?,cv_path=?", array($fields['FirstName'],$fields['LastName'],$fields['HomePhone'],$fields['WorkPhone'],$fields['MobilePhone'],$fields['Email'],$fields['Skype'],$fields['ContactMethod'],$fields['Location'],$fields['LocationOther'],$candiatefilename));

        $id = Jojo::insertQuery;

        //Jojo::insertQuery("INSERT INTO {db_candidate_job} SET candidateID=?,jobID=?,appliedDate=?", array($id,$jobid,date("d/m/y")));

        }
    }

2 个答案:

答案 0 :(得分:1)

它在Jojo文档中说。如果你想获得新插入的行的id,只需使用

function Insert()
{
  $emailCheck = $this->checkEmail($email);

  if($emailCheck != FALSE)
  {
    updateQuery by $emailCheck['id'])); // The email address is found so we update the data
    $id = $emailCheck['id'];
  }else{

    $id = Jojo::insertQuery; // The email address is not found so we insert the new row
  }
  //at this point $id holds the id of the row.
}

然后,在if / else语句之后,你可以在第二个表上运行代码,知道你有行的id,无论是哪种情况(INSERT / UPDATE)。

答案 1 :(得分:1)

尝试按照else语句中的以下顺序执行代码。

第一

$id = Jojo::insertQuery("INSERT INTO {db} SET first_name=?,last_name=?,home_phone=?,work_phone=?,mobile_phone=?,email=?,skype=?,contact_method=?,location=?,location_other=?,cv_path=?", array($fields['FirstName'],$fields['LastName'],$fields['HomePhone'],$fields['WorkPhone'],$fields['MobilePhone'],$fields['Email'],$fields['Skype'],$fields['ContactMethod'],$fields['Location'],$fields['LocationOther'],$candiatefilename));

第二

Jojo::insertQuery("INSERT INTO {db_candidate_job} SET candidateID=?,jobID=?,appliedDate=?", array($id,$jobid,date("d/m/y")));