如何从处理页面回显变量?

时间:2014-05-07 21:46:30

标签: php variables echo

我有一个生成PDF的单独文件,将选择信息保存到数据库,创建PDF然后通过电子邮件发送给用户。我想要做的是在确认页面上回显sql语句。

request.php - > PDF-generation.php - > confirmation.php

用户永远不会看到PDF生成页面。

这是功能:

function LogToDatabase($holding_id, $to_email, $from_email, $subject, $message, $pdf) { 
    //Save record of PDF here
    $db = new wpdb(DB_USER, DB_PASSWORD, 'webtransaction', DB_HOST);
    $sql = "INSERT INTO 504_pdf_storage (`holding_id`, `to_email`, `from_email`, `subject`, `message`, `pdf`) 
        VALUES(" . $holding_id . "," . $to_email ."," . $from_email ."," . $subject ."," .  $message ."," . $pdf . ");";
    return $db->insert_id;
}

我想将PDF-generation.php中的变量$ sql回显到confirmation.php上,但是一个简单的" echo $ sql;"给出错误说"未定义的变量"

请帮助和谢谢!

编辑:这是确认页面的顶部。     

require_once('../../../../wp-load.php');
require_once('../includes/phpmailer/class.phpmailer.php');
require_once('../classes/504-send-request.php');
require_once ('../classes/pdf_generator.php');

$mh = new MobileHomeRequest();
$data = $mh->GetTempData($_POST['id']);
$holding_id = $data->SaveToDatabase();
$pdf = new PDFGen(null,$data);
echo $sql;

get_header();

if(!$pdf->RenderPDF()) { ?>
    <script>
        alert('Couldn\'t send your email because our servers seem to be really lame today.');
    </script>
<?php } ?>

1 个答案:

答案 0 :(得分:0)

将值保存在会话变量中,然后在确认页面上回显出会话变量。您需要在每个页面的顶部添加session_start()

function LogToDatabase($holding_id, $to_email, $from_email, $subject, $message, $pdf) { 
  //Save record of PDF here
  $db = new wpdb(DB_USER, DB_PASSWORD, 'webtransaction', DB_HOST);
  $sql = "INSERT INTO 504_pdf_storage (`holding_id`, `to_email`, `from_email`, `subject`, `message`, `pdf`) 
    VALUES(" . $holding_id . "," . $to_email ."," . $from_email ."," . $subject ."," .  $message ."," . $pdf . ");";
  //add this
  $_SESSION['savedSQL'] = $sql;
  return $db->insert_id;
}

然后在确认页面上,这很简单:

echo htmlentities($_SESSION['savedSQL'], ENT_QUOTES); 

旁注

正如评论中所提到的,var_dump($sql)后跟exit()会好得多。另请注意htmlentities()以防止XSS,因为不知道查询中的变量来自何处。