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

如何使用php打印存储在mysql数据库中的特定数据,php,html,css,mysql,database,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:p

您好,我已经在我的网站上添加了一个功能,用户可以使用以下代码取消预订的机票: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');
?> 

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

  • 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代码中调用
    window.print()
    ,因为它是一个javascript函数

  • 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+MySQL+HTML+JS如何协同工作的基本知识

    目前,您不需要知道代码有什么问题。您需要学习一些基本教程,然后从头开始重新编写代码。互联网上有很多教程


    额外解释

    服务器=代码所在的位置

    客户端=浏览器

    PHP&MySQL仅在服务器中生活,在服务器上工作,由服务器处理

    HTML+CSS+JS由服务器准备,服务器将其发送给客户端,然后由客户端(浏览器)处理。因此,它们在客户端(浏览器)中开始工作。只要它们在服务器上,它们就只是字符串

    所以它总是像:

  • 来自服务器()的浏览器请求文件。这称为请求
  • 服务器运行php文件(something.php),该文件可能会生成输出(HTML+CSS+JS),然后服务器将输出发送到客户端(浏览器)。这称为响应
  • 客户端(浏览器)然后接收输出(作为普通字符串),然后浏览器运行代码(JS)

  • 结论:

    不要告诉服务器运行JS,不要告诉客户端(浏览器)运行PHP或MYSQL。

    立即停止编码

    您需要学习PHP+MySQL+HTML+JS如何协同工作的基本知识

    目前,您不需要知道代码有什么问题。您需要学习一些基本教程,然后从头开始重新编写代码。互联网上有很多教程


    额外解释

    服务器=代码所在的位置

    客户端=浏览器

    PHP&MySQL仅在服务器中生活,在服务器上工作,由服务器处理

    HTML+CSS+JS由服务器准备,服务器将其发送给客户端,然后由客户端(浏览器)处理。因此,它们在客户端(浏览器)中开始工作。只要它们在服务器上,它们就只是字符串

    所以它总是像:

  • 来自服务器()的浏览器请求文件。这称为请求
  • 服务器运行php文件(something.php),该文件可能会生成输出(HTML+CSS+JS),然后服务器将输出发送到客户端(浏览器)。这称为响应
  • 客户端(浏览器)然后接收输出(作为普通字符串),然后浏览器运行代码(JS)

  • 结论:


    不要告诉服务器运行JS,不要告诉客户端(浏览器)运行PHP或MYSQL。

    我已经修改了您的代码,使其能够工作,并且使用prepare语句更加安全

    <table>
    <tr><th> id </th> <th> time </th> </tr>
    
    <?php
    if (!$bk = $con->prepare("select * from tbl_bookings where user_id = ? ")) {
    echo $con->error; // show error message when SQL query is wrong or goes kaboom!
    } else{
    $bk->bind_param("s",$_SESSION['user']); //bind the blind parameters, "s" stands for string
    $bk->execute ();// execute the query
    $bk_result = $bk->get_result(); // get results 
    }
    while ($bk_row = $bk_result->fetch_assoc()){ ?>
    <tr><td> <?php echo $bk_row['id']; ?> </td> <td> <?php echo $bk_row['id'] ?> </td> </tr>
    
    <?php } //end while loop ?>
    </table>
    
    
    身份证时间
    
    我已经修改了您的代码,使用prepare语句使其工作起来更安全

    <table>
    <tr><th> id </th> <th> time </th> </tr>
    
    <?php
    if (!$bk = $con->prepare("select * from tbl_bookings where user_id = ? ")) {
    echo $con->error; // show error message when SQL query is wrong or goes kaboom!
    } else{
    $bk->bind_param("s",$_SESSION['user']); //bind the blind parameters, "s" stands for string
    $bk->execute ();// execute the query
    $bk_result = $bk->get_result(); // get results 
    }
    while ($bk_row = $bk_result->fetch_assoc()){ ?>
    <tr><td> <?php echo $bk_row['id']; ?> </td> <td> <?php echo $bk_row['id'] ?> </td> </tr>
    
    <?php } //end while loop ?>
    </table>
    
    
    身份证时间
    
    请注意,您的代码易受攻击,任何人都可以很容易地删除他们想要的任何预订!千万不要在SQL语句中直接使用
    $\u GET
    ——尤其是使用
    DELETE
    语句!您应该改为使用绑定到变量。有关如何在PHP中防止SQL注入的更多信息,请参阅:)另外,在
    profile.PHP
    上,
    $bkg
    与什么相关?输出ID的语句看起来正确;你真的要在
    $bkg['book_ID']
    中传递ID吗?我会知道这个thx,我会在我使用$bkg
    窗口的地方分享代码。print()
    是一个javascript函数,你不能在php脚本中调用它。在php中我怎么做?或者我该怎么做才能让它为这个工作?谢谢请注意,您的代码易受攻击,任何人都可以很容易地删除他们想要的任何预订!千万不要在SQL语句中直接使用
    $\u GET
    ——尤其是使用
    DELETE
    语句!您应该改为使用绑定到变量。有关如何在PHP中防止SQL注入的更多信息,请参阅:)另外,在
    profile.PHP
    上,
    $bkg
    与什么相关?输出ID的语句看起来正确;你真的要在
    $bkg['book_ID']
    中传递ID吗?我会知道这个thx,我会在我使用$bkg
    窗口的地方分享代码。print()
    是一个javascript函数,你不能在php脚本中调用它。在php中我怎么做?或者我该怎么做才能让它为这个工作?感谢致命错误:未捕获错误:调用第5行bool上的成员函数fetch_array()。我从问题中复制了您的代码,SQL命令有错误,应该是
    select*from…
    。我修正了我的答案。它确实有效,但是它没有从数据库中获取信息,我将上传一张显示它所显示内容的图片,如果您需要,请告诉我
    <table>
    <tr><th> id </th> <th> time </th> </tr>
    
    <?php
    if (!$bk = $con->prepare("select * from tbl_bookings where user_id = ? ")) {
    echo $con->error; // show error message when SQL query is wrong or goes kaboom!
    } else{
    $bk->bind_param("s",$_SESSION['user']); //bind the blind parameters, "s" stands for string
    $bk->execute ();// execute the query
    $bk_result = $bk->get_result(); // get results 
    }
    while ($bk_row = $bk_result->fetch_assoc()){ ?>
    <tr><td> <?php echo $bk_row['id']; ?> </td> <td> <?php echo $bk_row['id'] ?> </td> </tr>
    
    <?php } //end while loop ?>
    </table>