选择查询执行两次

时间:2019-04-05 21:46:26

标签: php mysqli

我正在写一个简单的页面来检索属于已登录用户的“ PO号码”。该部分工作正常,但查询返回了2套完整的记录。正如我读过的其他文章中所述,我已经检查了页面刷新问题,但没有发现。

poTable.php

    <table class="table" style="width: 100%">
        <thead class="thead-dark">
            <tr>
                <th scope="col">PO#</th>
                <th scope="col">Job#</th>
                <th scope="col">Job Name</th>
                <th scope="col">Salesperson</th>
                <th scope="col">Vendor</th>
                <th scope="col">Date</th>
            </tr>
        </thead>
        <tbody>

        <?php include 'usersPOs.php'?>

        </tbody>
    </table>

usersPOs.php

$db = mysqli_connect('localhost', 'root', '', 'acs-jobs-pos');

$userID = $_SESSION['userID'];
$userPrivs = $_SESSION['admin'];

if ($userPrivs == 0) {  // User IS NOT an admin
    // Get ALL main table records that belong to the logged-in user
    $sqlmain = "SELECT * FROM main INNER JOIN users ON main.userID = '" . $userID . "'";
    //debug_print_backtrace(); 
    $resultmain = mysqli_query($db,$sqlmain);
    while($rowmain = mysqli_fetch_assoc($resultmain))  
    {  
?>  
            <tr>  
<?php
        $PONumb = $rowmain["PONumb"];
        $jobNumber = $rowmain["jobNumber"];
        $jobName = $rowmain["jobName"];
        $salespersonID = $rowmain["salespersonID"];
        $vendorID = $rowmain["vendorID"];
        $timestamp = $rowmain["timestamp"];
?>

                <td><?php echo $PONumb ?></td>  
                <td><?php echo $jobNumber ?></td>  
                <td><?php echo $jobName ?></td>

<?php
        $sqlsp = "SELECT * FROM users WHERE userID = '" . $salespersonID . "'";
        $resultsp = mysqli_query($db,$sqlsp);   
        $rowsp = mysqli_fetch_assoc($resultsp);

        if ($rowsp["lastname"] == '' && $rowsp["firstname"] == '') {
?>
                <td></td>
<?php
        } else {
?>
                <td><?php echo $rowsp["lastname"] . ', ' . $rowsp["firstname"] ?></td>
<?php
        }

        $sqlvendor = "SELECT * FROM vendor 
                        WHERE vendorID = '" . $vendorID . "'";
        $resultvendor = mysqli_query($db,$sqlvendor);   
        $rowvendor = mysqli_fetch_assoc($resultvendor);
?>
            <td><?php echo $rowvendor["vendorname"] ?></td>

            <td><?php echo $timestamp ?></td>
        </tr>
<?php  
    }  
    mysqli_close($db);      
}

2 个答案:

答案 0 :(得分:0)

可以通过使用联接来极大地简化此代码。我还建议您使用PDO进行数据库访问;它不那么冗长,并且使准备好的语句更容易(并且这些语句在任何环境中都不是可选的。)仅从数据库中选择相关的列。 Heredoc块也用于简化输出。

<?php
$db = new PDO('mysql:host=localhost;dbname=acs-jobs-pos', 'root', '');

$userID = $_SESSION['userID'];
$userPrivs = $_SESSION['admin'];

if ($userPrivs == 0) {
    $sql = "SELECT m.PONumb, m.jobNumber, m.jobName, m.salespersonID, m.vendorID, m.timestamp,
        u.lastname, u.firstname, v.vendorname
        FROM main m
        LEFT JOIN users u ON (m.salespersonID = u.userID)
        LEFT JOIN vendors v ON (m.vendorID = v.vendorID)
        WHERE m.userID = ?";
    $stmt = $db->prepare($sql);
    $stmt->execute([$userID]);
    $results = $stmt->fetchAll(\PDO::FETCH_ASSOC);

    foreach ($results as $row) {
        echo <<< HTML
<tr>
    <td>$row[PONumb]</td>  
    <td>$row[jobNumber]</td>  
    <td>$row[jobName]</td>
    <td>$row[lastname], $row[firstname]</td>
    <td>$row[vendorname]</td>
    <td>$row[timestamp]</td>
</tr>
HTML;
    }
}

答案 1 :(得分:0)

跟随sql错误 “ SELECT * FROM main INNER JOIN用户ON main.userID ='”。 $ userID。 “'”;

您不能像这样使用join “选择*从主要的INNER JOIN用户打开main.userID = users.ID,其中users.ID ='”。 $ userID。 “'”;