簡體   English   中英

僅從 MySQL 查詢獲取會話數據的問題

[英]Issue getting only session data from MySQL query

我正在嘗試執行一個 MySQL 查詢,它僅從用戶在該會話期間創建的數據中選擇數據。 例如,用戶記錄了許多活動會話,並且當執行coast.php文件時,它僅在 PDF 輸出中顯示登錄用戶為該會話記錄的會話。

到目前為止,我已經設法使查詢在某種程度上起作用,但它仍在從數據庫中選擇所有內容,我無法弄清楚如何正確執行此操作。 任何幫助將不勝感激。

到目前為止,這是我的代碼...

海岸.php

<?php
session_start();
require_once('../tcpdf/examples/lang/eng.php');
require_once('../tcpdf/tcpdf.php');
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Ash Williams');
$pdf->SetTitle('Coastline Coasteer Invoice');
$pdf->SetSubject('ASHLEY');
$pdf->SetKeywords('wILLIAMS');
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// set default header data
$pdf->SetHeaderData("ibill logo.png", PDF_HEADER_LOGO_WIDTH, '', 
'');
//Set header margin
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
//set margins
$pdf->SetMargins(12, 35, 12, true);
//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
//set some language-dependent strings
$pdf->setLanguageArray($l);
// ---------------------------------------------------------
// set default font subsetting mode
$pdf->setFontSubsetting(true);
// Set font
// dejavusans is a UTF-8 Unicode font, if you only need to
// print standard ASCII chars, you can use core fonts like
// helvetica or times to reduce file size.
$pdf->SetFont('helvetica', '', 12, '', true);
// Add a page
// This method has several options, check the source code documentation for more information.
$pdf->AddPage();
// Set some content to print
$html = '<style>
            h1 {
                letter-spacing: 1px;
                 font-family: times;
                 font-size: 30px;
                 text-align: center;
                 text-transform: uppercase;
                 text-decoration:underline;
            }
            h2 {
                font-weight: normal;
                text-align: left;
            }
        </style>
                <h1>Invoice</h1>
                <h2>Coastline Coasteer</h2>

                <h3>22 Headland Road,</h3>

                <h3>Newquay, Cornwall</h3>
                <h3>TR7 1HN</h3>
                <h3>Tel: 01637 879571</h3>';

$tbl_header = '<table border="1" cellpadding="6" cellspacing="4" nobr="true">';
$tbl_footer = '</table>';
$tbl ='';
$tbl .= '
          <tr>
            <th style="border: 3px solid black";>Type of Activity</th>
            <th style="border: 3px solid black";>Employer</th>
            <th style="border: 3px solid black";>Date</th>
            <th style="border: 3px solid black";>Time</th>
            <th style="border: 3px solid black";>Amount (GBP)</th>
          </tr>
        ';
$con=mysqli_connect("localhost","root","****","****");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
$tusername=$_SESSION["user_session"];
$queryString = "SELECT * FROM session_details, users WHERE employer = 'Coastline Coasteer' AND username = '" .$tusername. "';"; 
//print $queryString;
$result = mysqli_query($con,$queryString);
//if (!$result) {
//  print 'failed';
//}

while($row = mysqli_fetch_array($result))
  {
  $typeofactivity = $row['typeofactivity'];
  $employer = $row['employer'];
  $date = $row['date'];
  $time = $row['amount'];
  $tbl .= '<tr>
            <td>'.$row["typeofactivity"].'</td>
            <td>'.$row["employer"].'</td>
            <td>'.$row["date"].'</td>
            <td>'.$row["time"].'</td>
            <td>'.$row["amount"].'</td>
          </tr>';
}
// Print text using writeHTMLCell()
$pdf->writeHTML($html . $tbl_header . $tbl . $tbl_footer, true, false, false, false, '');
// ---------------------------------------------------------
// Close and output PDF document
// This method has several options, check the source code documentation for more information.
$pdf->Output('coastline_invoice.pdf', 'D');
//============================================================+
// END OF FILE
//============================================================+
?>

問題是您在 session_details 和 users 表之間既沒有隱式也沒有顯式連接條件。 沒有任何連接條件的session_details, users語法將在 2 個表之間創建 Carthesian 連接,這意味着 session_details 表中的每條記錄都與 users 表中的每條記錄相匹配。

您必須在 2 個表之間定義連接條件,如下所示:

SELECT *
FROM session_details sd
INNER JOIN users u ON sd.user_id=u.user_id
WHERE employer = 'Coastline Coasteer' AND username = '...'

我只是在連接表達式中編造了字段名稱,但重點是,在 session_details 表中應該通過一個公共字段以某種方式與用戶表相關聯。 您需要確定該字段是什么(或這些字段是什么)以將 2 個表中的相應記錄相互匹配。

您在這里遇到的問題是您的會話沒有保存任何信息。 在行中:

$tusername=$_SESSION["user_session"];

您指的是 user_session 值,該值似乎未在此頁面的任何位置設置。 您可以嘗試使用print_r($_SESSION)打印出 $_SESSION 變量。 然而,$_SESSION 只是一個數組,因此除非您在這里存儲一些值,否則您只是指一個空值,因此當您運行實際查詢時,您正在查詢一個空值。

您可以使用一個新的測試 php 頁面來演示這一點,該頁面僅包含以下內容:

<?php
session_start();
print_r($_SESSION);

您的輸出將是: Array()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM