如何插入主键第二个表

时间:2017-04-15 17:05:50

标签: php mysql sql-insert

我正在从单个html表单中检索数据并将其插入到两个不同的SQL表USERPAYMENT_DETAILS中,我在PAYMENT_DETAILS表中创建了一个外键,现在我很困惑如何将USER表的主键添加到PAYMENT_DETAILS

 if (isset($_POST['submit']))
{

  $firstname=$_POST['firstname'];
  $lastname=$_POST['lastname'];
  $gender=$_POST['gender'];
  $datofbirth=$_POST['dateofbirth'];
  $primary_email=$_POST['primary_email'];
  $Email_Confirm=$_POST['Email_Confirm'];
  $phone=$_POST['contact'];
  $password=$_POST['password'];
  $confirmpassword=$_POST['confirmpassword'];
  $username= $_POST['username'];
  $status=2;

  //payment information 
  $ownername=$_POST['ownername'];
  $cvvnumber=$_POST['cvvnumber'];
  $cardnumber=$_POST['cardnumber'];
  $cardtype=$_POST['cardtype'];
  $expirydate=$_POST['expirydate'];
  $balance=$_POST['money'];

   $mysql_get_users = mysql_query("SELECT * FROM user where username='$username'");
  $get_rows = mysql_affected_rows($conn);

    if($get_rows >=1)
        {
          echo "user exists";die();
        }     
  else
    {
         $query1= mysql_query("
         INSERT INTO 
         user
            (firstname,lastname,gender,datofbirth,primary_email,contact,password,username,status)

              VALUES
                    ('$firstname','$lastname','$gender','$datofbirth','$primary_email','$phone','$password','$username','$status')");

    $query2 = mysql_query("
      INSERT INTO 
      payment_details
                    (Owner_Name,CVV_Numer,Card_Number,Card_Balance,Card_Type,Validation)
       VALUES 
             ('$ownername','$cvvnumber','$cardnumber','$balance','$cardtype','$expirydate')"); 

1 个答案:

答案 0 :(得分:0)

php函数给出了最后输入的记录的主键。(在第一次查询之后放置该函数。这意味着,它将给出第一个输入的主键。)

 mysql_insert_id();