php - 如何使用php打印存储在mysql数据库中的特定数据

标签 php html css mysql database

您好,我在我的网站上添加了一个功能,用户可以使用以下代码取消预订的机票:cancel.php

<?php
session_start();
include('config.php');
mysqli_query($con,"delete from tbl_bookings where book_id='".$_GET['id']."'");
$_SESSION['success']="Booking Cancelled Successfully";
header('location:profile.php');
?> 

我尝试在同一张票上添加一个功能,用户可以取消打印票,这样用户就可以打印这张票了,我使用的代码是:print.php

<?php
session_start();
include('config.php');
window.print(mysqli_query($con,"select from tbl_bookings where book_id='".$_GET['id']."'"));
header('location:profile.php');
?>

在一个名为 profile.php 的类中指向这两个类的链接,这一点位于它表示的行中:

                            <a href="cancel.php?id=<?php echo $bkg['book_id'];?>">Cancel </a>/<a href="print.php?id=<?php echo $bkg['book_id'];?>">Print Ticket</a>

如果你能告诉我如何打印这些数据我会很高兴..谢谢

$bkg的使用

$bk=mysqli_query($con,"select * from tbl_bookings where user_id='".$_SESSION['user']."'");
                if(mysqli_num_rows($bk))
                {
                    ?>
                    <table class="table table-bordered">
                        <thead>
                        <th>Booking Id</th>
                        <th>Movie</th>
                        <th>Theatre</th>
                        <th>Screen</th>
                        <th>Show</th>
                        <th>Seats</th>
                        <th>Price</th>
                        <th></th>
                        </thead>
                        <tbody>
                        <?php
                        while($bkg=mysqli_fetch_array($bk))
                        {
                            $m=mysqli_query($con,"select * from tbl_movie where movie_id=(select movie_id from tbl_shows where s_id='".$bkg['show_id']."')");
                            $mov=mysqli_fetch_array($m);
                            $s=mysqli_query($con,"select * from tbl_screens where screen_id='".$bkg['screen_id']."'");
                            $srn=mysqli_fetch_array($s);
                            $tt=mysqli_query($con,"select * from tbl_theatre where id='".$bkg['t_id']."'");
                            $thr=mysqli_fetch_array($tt);
                            $st=mysqli_query($con,"select * from tbl_show_time where st_id=(select st_id from tbl_shows where s_id='".$bkg['show_id']."')");
                            $stm=mysqli_fetch_array($st);
                            ?>
                            <tr>
                                <td>
                                    <?php echo $bkg['ticket_id'];?>
                                </td>
                                <td>
                                    <?php echo $mov['movie_name'];?>
                                </td>
                                <td>
                                    <?php echo $thr['name'];?>
                                </td>
                                <td>
                                    <?php echo $srn['screen_name'];?>
                                </td>
                                <td>
                                    <?php echo $stm['start_time'];?>
                                    <?php echo $stm['name'];?>
                                </td>
                                <td>
                                    <?php echo $bkg['no_seats'];?>
                                </td>
                                <td>
                                    £   <?php echo $bkg['amount'];?>
                                </td>
                                <td>
                                    <?php  if($bkg['ticket_date']<date('Y-m-d'))
                                    {
                                        ?>
                                        <i class="glyphicon glyphicon-ok"></i>
                                        <?php
                                    }
                                    else
                                    {?>
                                    <a href="cancel.php?id=<?php echo $bkg['book_id'];?>">Cancel </a>/<a href="print.php?id=<?php echo $bkg['book_id'];?>">Print Ticket</a>
                                    <?php
                                    }
                                    ?>
                                </td>
                            </tr>
                            <?php
                        }
                        ?></tbody>

enter image description here

最佳答案

  1. 您不能在 PHP 代码中调用 window.print(),因为它是一个 javascript 函数

  2. header('location:profile.php'); 将在 javascript 有机会执行代码之前重定向页面。用打印页面后执行的 javascript 代码替换该代码。

你的 print.php:

<?php
session_start();
include('config.php');
$result = mysqli_query($con, "select * from tbl_bookings where book_id='{$_GET['id']}'"); // You should replace this with  prepare statement
$row = $result->fetch_array();
// assume that your booking table has columns: id, movie_name, time
echo "<table>
<tr><td>Booking ID</td><td>{$row['id']}</td></tr>
<tr><td>Movie Name</td><td>{$row['movie_name']}</td></tr>
<tr><td>Time</td><td>{$row['time']}</td></tr>
</table>";   
?>
<script>
   window.print();
   window.location.href = "profile.php"
</script>

关于php - 如何使用php打印存储在mysql数据库中的特定数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55013986/

相关文章:

javascript - Ajax注册失败不回显结果

php - ZF2 : run MySQL command "SET SQL_BIG_SELECTS=1" before executing SQL-statements

html - 旁边漂浮着描述图片的文字

javascript - 如何使用jQuery在文本框中获取点击的单词索引

javascript - 如何选择非空值输入并隐藏输入并显示另一个输入?

php - 注册时生成的哈希与登录时生成的哈希不同

php - JSON 数据到 UL

当我在某个点向下滚动或刷新时,css 固定位置 div 消失

jquery滚动问题

html - 如何在每次请求时自动预编译 css.erb 文件