当用户从 Table 登录时 1 仅显示他来自 Table 2 的数据

when user signed in from Table 1 show only his data from Table 2

我的数据库中有两个 table。
userTable (user_id, companyName, pass) 包含所有用户及其公司名称,登录数据来自此处。
productionTable(production_id, companyName, deliveryNotes) 也有,公司名称和送货单。

这里的主要思想是,每当用户使用他的公司名称登录时,将只能看到他的 table。

我想做如下事情:

if (userInput === userInsideTable && companyInput === companyInsideTable) {show only his information}

下面是我的登录信息 所以现在我应该在登录或其他页面中应用这里的逻辑

<?php
session_start();

$conn = mysqli_connect('localhost', 'root', '', 'test_db1');

if (!$conn) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    exit();
} else {
    if (isset($_POST['myS'])) {
        $myU = mysqli_real_escape_string($conn, $_POST['myU']);
        $myP = mysqli_real_escape_string($conn, $_POST['myP']);
        $myC = mysqli_real_escape_string($conn, $_POST['myC']);

        $sql = "SELECT * FROM userTable WHERE myUsername = ? AND myPassword = ? AND myCompany = ?";

        $stmt = $conn->prepare($sql);
        $stmt->bind_param("sss", $myU, $myP, $myC);
        $stmt->execute();
        $result = $stmt->get_result();
        $row = $result->fetch_assoc();

        if ($result->num_rows === 1) {
            session_regenerate_id();
            $_SESSION['myU'] = $row['myUsername'];
            $_SESSION['myC'] = $row['myCompany'];
            session_write_close();
            echo ("<script>window.location = 'test.php';</script>");
        } else {
            echo ("<script>alert('Username/Password is Incorrect!')</script>");
            echo ("<script>window.location = 'index.php';</script>");
        }
    }
}
?>
<form action="index.php" method="post">
            username<input type="text" name="myU" id="">
            pass <input type="password" name="myP" id="">
            company<input type="text" name="myC" id="">
            <input type="submit" value="login" name="myS">
        </form>

这是我的 test.php

<?php
session_start();

$con = mysqli_connect('localhost', 'root', '', 'test_db1');
// Check connection
if (!$con) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
} else {
    $result = mysqli_query($con, "SELECT * FROM productionTable WHERE company = " . $_SESSION['myC'] . " ");


    while ($row = mysqli_fetch_assoc($result)) {
        echo $row["production_id"];
        echo $row["company"];
        echo  $row["notes"];
    }
}

mysqli_close($con);

仅获取登录用户信息的最佳方法是什么...
如果公司 x1 的用户 1 密码 123 登录 he/she 只能看到来自另一个 table

的相关信息

谁能提出一个不同的逻辑
例如
登录页面和结果页面
登录页面是用户帐户管理和结果页面制作table 或者可能根本不是

该问题详细说明了两个 table 中的某些列,但 test.php 中的 sql 引用了迄今为止未声明的列 - company。假设列的初始声明是正确的并且两个 table 都包含 companyName 详细信息,那么应该可以使用该公共列在 table 之间伪造一个 join .

下面的

None是经过测试的,如有错误请见谅。关于用户密码,您需要解决一些问题 - 它应该根据评论进行哈希处理,这会影响 index.php 中用于确定会话变量的逻辑。

test.php 中的 sql 试图在两个 table 之间创建联合,从而将用户与公司联系起来。使用适当的 [=28 会更容易=] 架构和数据!注意 hte sql 使用别名作为 table 名称以使 sql 更易于阅读。

<?php
    
    #index.php
    
    session_start();
    
    if( $_SERVER['REQUEST_METHOD']=='POST' && isset( 
        $_POST['myS'],
        $_POST['myU'],
        $_POST['myP'],
        $_POST['myC']
    )) {
    
        $conn = mysqli_connect('localhost', 'root', '', 'test_db1');
    
        $myU = $_POST['myU'];
        $myP = $_POST['myP'];
        $myC = $_POST['myC'];
        /*
        
            user password should be hashed!
            
        */
        $sql = "SELECT count(*) as `count` FROM `userTable` WHERE `myUsername` = ? AND `myPassword`=? and `myCompany` = ?";
        $stmt = $conn->prepare( $sql );
        $stmt->bind_param( "sss", $myU, $myP, $myC );
        $stmt->execute();
        $stmt->store_result();
        $stmt->bind_result($count);
        
        if( $count==1 && $stmt->num_rows==1 ){
        
            session_regenerate_id();
            $_SESSION['myU'] = $myU;
            $_SESSION['myC'] = $myC;
            session_write_close();
            
            exit(header('Location: test.php'));
        }
        
        
        echo "
        <script>
            alert('Username/Password is Incorrect!');
            location.search = 'error=true';
        </script>";
    }
    
?>

并且 test.php 文件可能是...

<?php
    
    #test.php
    
    session_start();
    if( isset(
        $_SESSION['myU'],
        $_SESSION['myC']
    ) ){
        $con = mysqli_connect('localhost', 'root', '', 'test_db1');
        
        $sql='select * from `userTable` u
                inner join `productionTable` p on p.companyName=u.companyName
                where p.companyName=? and u.myUsername=?';
        $stmt=$con->prepare($sql);
        $stmt->bind_param('ss', $_SESSION['myC'], $_SESSION['myU'] );
        $stmt->execute();
        $result=$stmt->get_result();
        
        while( $rs = $result->fetch_array( MYSQLI_ASSOC ) ){
            echo $rs[''];
        }
    }