在连接MySQL和PHP时将HTML代码存储在变量中

时间:2017-03-21 14:22:56

标签: php mysql concatenation

我想要存储在变量中的代码块,所以我可以使用dompdf将其转换为PDF

$html = '<div class="receiptContainer">
        <center>
            <img src="Images/logo.png" width="175px">
            <h4>GOKUJOU JAPANESE RESTAURANT</h4>
            <p>Total Gas Station, Hibbard Ave., Looc,<br>Dumaguete City, 6200 Negros Oriental, Philippines <br>
            09985555175 | 422-1435 <br>
            <?php echo date("Y-m-d h:i:sA"); ?>
            </p>

            <table width="90%" style="text-align: center;">
                <tr>
                    <th>DESCRIPTION</th>
                    <th>QTY</th>
                    <th>PRICE</th>
                    <th>TOTAL</th>
                </tr>
                <tr>
                    <td></td>
                </tr>
                <?php
                    $query = mysqli_query($con, "SELECT * FROM orders WHERE customerID = '".$_SESSION['customer']."' AND status = '"Checked Out"'");
                    while($row = mysqli_fetch_row($query)){
                ?>
                <tr>
                    <td><?php echo $row[3]; ?></td>
                    <td><?php echo $row[5]; ?></td>
                    <td><?php echo $row[4]; ?></td>
                    <td><?php echo $row[6]; ?></td>
                </tr>
                <?php
                    }
                    $total = mysqli_query($con, "SELECT SUM(total) AS grandTotal FROM orders WHERE customerID = '".$_SESSION['customer']."' AND status = '"Checked Out"' GROUP BY customerID");
                    $row = mysqli_fetch_row($total);
                    $sum = $row[0];
                ?>
                <tr>
                    <!-- break space -->
                    <tr></tr><tr></tr><tr></tr><tr></tr>
                    <tr></tr><tr></tr><tr></tr><tr></tr>
                    <tr></tr><tr></tr><tr></tr><tr></tr>

                    <td colspan="1" style="text-align: left">GRAND TOTAL: <?php echo $sum; ?></td>
                    <td colspan="3"></td>
                </tr>
                <tr style="text-align: left">
                    <td colspan="1">CASH: <?php echo $_SESSION['"cash"']; ?></td>
                    <td colspan="3"></td>
                </tr>
                <tr style="text-align: left">
                    <td colspan="1">CHANGE: <?php echo $_SESSION['"cash"'] - $sum; ?></td>
                    <td colspan="3"></td>
                </tr>
            </table>
        </center>
    </div>';
//start PDF generation
$dompdf = new Dompdf();
$dompdf->loadHTML($html);
$dompdf->setPaper(array(0, 0, 1080, 500), 'landscape');
$dompdf->render();
$dompdf->stream("samplepdf");
?>

这是我构建代码的方式,它给我一个错误:

  

解析错误:语法错误,意外'''in   第107行的C:\ xampp \ htdocs \ Gokujou \ checkout.php

这是第107行:

$query = mysqli_query($con, "SELECT * FROM orders WHERE customerID = '".$_SESSION['customer']."' AND status = '"Checked Out"'");

如何正确连接这个MySQL语句?

2 个答案:

答案 0 :(得分:1)

如果您使用“,只有在使用”时,您不需要在php中使用连接运算符。您可以像这样转换您的作业:

$query = mysqli_query($con, "SELECT * FROM orders WHERE customerID = '$_SESSION['customer']' AND status = 'Checked Out'");

但是我们可以将你所做的事情转化为这个(用运营商来解决)

$query = mysqli_query($con, "SELECT * FROM orders WHERE customerID = '".$_SESSION['customer']."' AND status = 'Checked Out'");

答案 1 :(得分:1)

我认为第107行中的错误是由于您打开字符串“而且还有未检出的引号”“已检出”

要逃避引号,但在它们之前反斜杠。即。

$query = mysqli_query($con, "SELECT * FROM orders WHERE customerID = '".$_SESSION['customer']."' AND status = '\"Checked Out\"'");

顶部的大$html部分最好用Heredoc表示。这样可以在多行上提供更清晰的代码,并且无需转义引号。

$html = <<<HTML
<div class="receiptContainer">
    <center>
    ...
HTML;

最后,您在MySQL查询中直接使用会话变量,而不进行任何清理。如果你不小心,这可能导致SQL注入攻击。