将登录用户的ID作为外键插入

时间:2016-03-08 01:39:19

标签: php mysqli

我有一个表单可以将值插入tbl_locators但我需要登录用户的ID才能引用它

如何获取登录用户的ID并将其作为FK插入以引用tbl_clients

它与Sessions有什么关系吗?

登录.php

    <?php

include('dbconn.php'); // Database connection and settings

// checking the user

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

$user = mysqli_real_escape_string($con,$_POST['user']);

$pass = mysqli_real_escape_string($con,$_POST['pass']);

$sel_ID = "select accholder_ID from tbl_accholder where accholder_Username='$user' AND accholder_Password='$pass'";
$sel_user = "select * from tbl_accholder where accholder_Username='$user' AND accholder_Password='$pass'";

$run_user = mysqli_query($con, $sel_user);
$run_ID = mysqli_query($con, $sel_ID);

$check_user = mysqli_num_rows($run_user);

if($check_user==1){

$_SESSION['accholder_Username']= $user;
$_SESSION['accholder_ID']= $run_ID;

echo $_SESSION['accholder_ID'];

}

else {

echo "<script>alert('Username or password is not correct, try again!')</script>";

}

}

?>

注册.php

    <?php
session_start();
include('dbconn.php'); // Database connection and settings

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

    $accholder_ID   = $_SESSION['accholder_ID'];
    $companyname    = $_POST["companyname"];
    $nationality    = $_POST["nationality"];
    $primaryaddress = $_POST["primaryaddress"];
    $industrycode   = $_POST["industrycode"];
    $tin            = $_POST["tin"];
    $status         = $_POST["status"];
    $category       = $_POST["category"];
    $employees      = $_POST["employees"];
    $email          = $_POST["email"];
    $mobile         = $_POST["mobile"];
    $telephone      = $_POST["telephone"];

    $companyname    = trim(mysqli_escape_string($con, $companyname));
    $nationality    = trim(mysqli_escape_string($con, $nationality));
    $primaryaddress = trim(mysqli_escape_string($con, $primaryaddress));
    $industrycode   = trim(mysqli_escape_string($con, $industrycode));
    $tin            = trim(mysqli_escape_string($con, $tin));
    $status         = trim(mysqli_escape_string($con, $status));
    $category       = trim(mysqli_escape_string($con, $category));
    $employees      = trim(mysqli_escape_string($con, $employees));
    $email          = trim(mysqli_escape_string($con, $email));
    $mobile         = trim(mysqli_escape_string($con, $mobile));
    $telephone      = trim(mysqli_escape_string($con, $telephone));


    $sql     = "SELECT locators_CompanyName FROM tbl_locators WHERE locators_CompanyName='$companyname'";
    $sql2    = "SELECT locatorsCon_email FROM tbl_locatorscon WHERE locatorsCon_email='$email'";
    $result  = mysqli_query($con, $sql);
    $result2 = mysqli_query($con, $sql2);
    $row     = mysqli_fetch_array($result, MYSQLI_ASSOC);
    $row2    = mysqli_fetch_array($result2, MYSQLI_ASSOC);

    if (mysqli_num_rows($result) == 1) {
        echo "Sorry...This Company already exist..";
    } else if (mysqli_num_rows($result2) == 1) {
        echo "Sorry...This Email already exist..";
    } else {
        $query = mysqli_query($con, "INSERT INTO tbl_locators( locators_CompanyName, locators_Nationality, locators_PrimaryAddress, locators_IndustryCode, locators_TIN, locators_Status, locators_Category, locators_Employees, accholder_ID) 
VALUES ('$companyname', '$nationality', '$primaryaddress', '$industrycode','$tin','$status','$category','$employees', '$accholder_ID')");

        $LIid = $query = mysqli_insert_id($con);

        $query2 = mysqli_query($con, "INSERT INTO tbl_locatorscon( locatorsCon_email, locatorsCon_mobile, locatorsCon_telephone, locators_ID) 
VALUES ('$email', '$mobile', '$telephone', $LIid)");
        if ($query && $query2) {
            echo "Thank You! you are now registered.";
        }
        else 
            echo "Error";

    }
}

?>

1 个答案:

答案 0 :(得分:0)

由于使用单引号,此行将导致语法错误为'$_SESSION['accholder_ID']'

$query = mysqli_query($con, "INSERT INTO tbl_locators( locators_CompanyName, locators_Nationality, locators_PrimaryAddress, locators_IndustryCode, locators_TIN, locators_Status, locators_Category, locators_Employees, acchold_ID) 
VALUES ('$companyname', '$nationality', '$primaryaddress', '$industrycode','$tin','$status','$category','$employees', '$_SESSION['accholder_ID']')");

您可以将$_SESSION['accholder_ID']声明为变量,即$accholder_ID,然后再插入$accholder_ID

<强>更新

您还需要使用单引号包装$LIid

$query2 = mysqli_query($con, "INSERT INTO tbl_locatorscon( locatorsCon_email, locatorsCon_mobile, locatorsCon_telephone, locators_ID) 
VALUES ('$email', '$mobile', '$telephone', $LIid)");

未设置变量$_SESSION['accholder_Username']= $user$_SESSION['accholder_ID']= $run_user,因为session_start()未声明login.php

这导致了错误:

  

注意:未定义的索引:第7行的C:\ xampp \ htdocs \ Cisu \ register.php中的accholder_ID

您错误地设置了$_SESSION['accholder_ID']= $run_ID$_SESSION['accholder_ID']= $run_user

mysqli_query将返回对象,而不是字符串

因此,您需要循环才能获取变量。 $run_ID$run_user将成为字符串。

您还只需要一个查询:

$sel_user = "select * from tbl_accholder where accholder_Username='$user' AND accholder_Password='$pass'";

$run_user = mysqli_query($con, $sel_user);

$check_user = mysqli_num_rows($run_user);

if($check_user==1){

while ($row = mysqli_fetch_array($run_user)) {
    $run_ID = $row['accholder_ID'];
    $run_user = $row['accholder_Username'];
}

$_SESSION['accholder_Username']= $user;
$_SESSION['accholder_ID']= $run_ID;

} else {

[other code ...]