【问题标题】:Storing HTML code in a variable while concatenating MySQL and PHP在连接 MySQL 和 PHP 时将 HTML 代码存储在变量中
【发布时间】:2017-08-13 06:40:34
【问题描述】:

我有一段代码要存储在一个变量中,这样我就可以使用 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 C:\xampp\htdocs\Gokujou\checkout.php 在第 107 行

这是第 107 行:

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

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

【问题讨论】:

  • 我可以建议你使用一些 IDE 吗?因为在断言数据时代码中存在错误

标签: php mysql concatenation


【解决方案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'");

【讨论】:

    【解决方案2】:

    我认为第 107 行的错误是因为您使用 " 打开字符串但在 "Checked Out" 周围还有未转义的引号

    转义引号,但在它们前面加一个反斜杠。即

    $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 注入攻击。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-05
      • 2017-09-01
      • 1970-01-01
      • 2013-05-23
      • 1970-01-01
      • 2011-07-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多