将第一个表的自动增量id作为另一个表的外部id同时获取

时间:2016-10-22 13:22:53

标签: php mysql

我为一个表单创建了两个不同的表。将表格的上半部分插入一个表格中,然后将表格的一部分插入另一个表格中。 我必须将第一个表的主要ID 作为另一个表的外国ID

有可能吗?那是怎么回事? 这是php的查询代码

    if(isset($_POST['add_hostel']))
{
    extract($_POST);
    $checkbox = implode(',',$_POST['checkbox']);
    $checkbox1 = implode(',',$_POST['checkbox1']);
    $insert_details = mysql_query("INSERT INTO `add_hostel` (owner_id,accomodation_type,hostel_for,hostel_name,owner_name,email_id,contact_number,alternate_number,address,city,website) VALUES 
                                ('".$_SESSION['login_id']."','".$checkbox."','".$checkbox1."','".$hostel_name."','".$ownername."','".$email."','".$contact_no."','".$phonealternate."','".$address."','".$searchTextField."','".$hostel_website."')") or die(mysql_error());        
}

if(isset($_POST['add_hostel']))
{   
    extract($_POST);
    $hostel_id = mysql_query("SELECT LAST_INSERT_ID()");

    $checkbox2 = implode(',',$_POST['checkbox2']);
    $checkbox3 = implode(',',$_POST['checkbox3']);

    $insert_details1 = mysql_query("INSERT INTO `hostel_features` (hostel_id,facilities_info,food_type,breakfast,lunch,dinner,special) VALUES 
                                ('".$hostel_id."','".$checkbox2."','".$checkbox3."','".$breakfast."','".$lunch."','".$dinner."','".$special."')") or die(mysql_error());
}

1 个答案:

答案 0 :(得分:1)

自PHP 5.5.0起,

The php mysql extension已被弃用,自PHP 7.0.0起已被删除

而是使用the mysqli extension,它允许您访问MySQL 4.1及更高版本提供的功能。

您可以使用insert_id

解决问题
Private Sub btnlogin_Click(sender As Object, e As EventArgs) Handles btnlogin.Click

    Dim rd As SqlDataReader
    Dim name As String
    Dim Password As String
    Dim tsPass As String
    Dim userName As String
    Dim type As String

    connect()
    cmd = New SqlCommand("SELECT * from tblUser", cn)

    rd = cmd.ExecuteReader
    If rd.HasRows Then
        While rd.Read()
            name = rd("name").ToString()
            Password = rd("password").ToString()
            userName = rd("username").ToString()
            type = rd("usertype").ToString()

            tsPass = tbpassword.Text()
            frmMain.lblUser.Text = name

            If Password = tsPass And userName = tbusername.Text Then
                MetroMessageBox.Show(Me, "Logged in successfully as " & type, "Authentication Approved", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Me.Hide()
                frmMain.ShowDialog()

                tbpassword.Text = ""
                tbusername.Text = ""
            Else
                'MetroMessageBox.Show(Me, "Username and Password do not match..", "Authentication Failure", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

                tbpassword.Text = ""
                tbusername.Text = ""
                MsgBox("Username and Password do not match..", "Authentication Failure", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) 'THIS MESSAGE POPUP TWICE
            End If
        End While
    End If

End Sub

如果由于某种原因你想坚持使用mysql,程序方式几乎是一样的:

$hostel_id = $mysqli->insert_id; // or  mysqli_insert_id() for procedural style